You are here

function file_uri_to_object in File Entity (fieldable files) 7.2

Same name and namespace in other branches
  1. 8.2 file_entity.module \file_uri_to_object()
  2. 7.3 file_entity.file_api.inc \file_uri_to_object()
  3. 7 file_entity.file_api.inc \file_uri_to_object()

Returns a file object which can be passed to file_save().

@todo This should probably be named file_load_by_uri($uri, $create_if_not_exists). @todo Remove this function when http://drupal.org/node/685818 is fixed.

Parameters

string $uri: A string containing the URI, path, or filename.

bool $use_existing: (Optional) If TRUE and there's an existing file in the {file_managed} table with the passed in URI, then that file object is returned. Otherwise, a new file object is returned. Default is TRUE.

Return value

object|bool A file object, or FALSE on error.

3 calls to file_uri_to_object()
file_entity_add_upload_multiple_submit in ./file_entity.pages.inc
Submit handler for the multiple upload form.
file_entity_file_download in ./file_entity.module
Implements hook_file_download().
file_entity_generate_file_batch_generate in ./file_entity.devel_generate.inc
Implements hook_generate_file_batch_generate() using Devel generate api.

File

./file_entity.file_api.inc, line 746
API extensions of Drupal core's file.inc.

Code

function file_uri_to_object($uri, $use_existing = TRUE) {
  $file = FALSE;
  $uri = file_stream_wrapper_uri_normalize($uri);
  if ($use_existing) {

    // We should always attempt to re-use a file if possible.
    $files = entity_load('file', FALSE, array(
      'uri' => $uri,
    ));
    $file = !empty($files) ? reset($files) : FALSE;
  }
  if (empty($file)) {
    $file = new stdClass();
    $file->uid = $GLOBALS['user']->uid;
    $file->filename = drupal_basename($uri);
    $file->uri = $uri;
    $file->filemime = file_get_mimetype($uri);

    // This is gagged because some uris will not support it.
    $file->filesize = @filesize($uri);
    $file->timestamp = REQUEST_TIME;
    $file->status = FILE_STATUS_PERMANENT;
  }
  return $file;
}