? file_uri_to_object.685818.9.patch ? includes/.directory Index: includes/file.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/file.inc,v retrieving revision 1.243 diff -u -p -r1.243 file.inc --- includes/file.inc 15 Dec 2010 03:39:41 -0000 1.243 +++ includes/file.inc 13 Jan 2011 16:24:47 -0000 @@ -2057,6 +2057,41 @@ function file_get_mimetype($uri, $mappin } /** + * Returns a file object which can be passed to file_save(). + * + * @param $uri + * A string containing the URI, path, or filename. + * @return + * A file object, or FALSE on error. + */ +function file_uri_to_object($uri) { + $query = db_select('file_managed', 'f') + ->fields('f', array('fid')) + ->condition('uri', $uri) + ->execute() + ->fetchCol(); + if (!empty($query)) { + $file = file_load(array_shift($query)); + } + else { + 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; +} + +/** * Set the permissions on a file or directory. * * This function will use the 'file_chmod_directory' and 'file_chmod_file'