Should probably use Drupal's file api.  https://api.drupal.org/api/drupal/includes!file.inc/group/file/7