You are here

function file_styles_uri_to_object in Styles 7.2

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

Parameters

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

$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.

Return value

A file object, or FALSE on error.

See also

http://drupal.org/node/685818

2 calls to file_styles_uri_to_object()
file_styles_styles_filter in contrib/file_styles/file_styles.module
Styles filter callback.
theme_file_styles_image_preview in contrib/file_styles/includes/themes/file_styles.theme.inc
Preview image for Styles UI.

File

contrib/file_styles/file_styles.module, line 137
styles/contrib/file_styles/file_styles.module File widget formatter definitions.

Code

function file_styles_uri_to_object($uri, $use_existing = FALSE) {
  if ($use_existing) {
    $query = db_select('file_managed', 'f')
      ->fields('f', array(
      'fid',
    ))
      ->condition('uri', $uri)
      ->execute()
      ->fetchCol();
    if (!empty($query)) {
      $file = file_load(array_shift($query));
    }
  }
  if (!isset($file)) {
    global $user;
    $uri = file_stream_wrapper_uri_normalize($uri);
    $wrapper = file_stream_wrapper_get_instance_by_uri($uri);
    $file = new StdClass();
    $file->uid = $user->uid;
    $file->filename = 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;
    $file->is_new = TRUE;
  }
  return $file;
}