1 file_example.module file_example_get_managed_file($uri)

Utility function to check for and return a managed file.

In this demonstration code we don't necessarily know if a file is managed or not, so often need to check to do the correct behavior. Normal code would not have to do this, as it would be working with either managed or unmanaged files.

Parameters

string $uri: The URI of the file, like public://test.txt.

Related topics

File

modules/examples/file_example/file_example.module, line 506
Examples demonstrating the backdrop File API (and Stream Wrappers).

Code

function file_example_get_managed_file($uri) {
  $fid = db_query('SELECT fid FROM {file_managed} WHERE uri = :uri', array(':uri' => $uri))->fetchField();
  if (!empty($fid)) {
    $file_object = file_load($fid);
    return $file_object;
  }
  return FALSE;
}