Hello,

is there a way to load a file by uri?

i can't find any function like this in the API...

thanks

[SOLUTION]
Well, i knew i could do it via select from DB... so, here's the code:

$result = db_query('SELECT f.fid
              FROM {file_managed} f WHERE f.uri = :uri', array(':uri' => $uri));

[/SOLUTION]

Comments

joeysantiago’s picture

perhaps, i should describe the situation a little bit more.

I'm hooking node_presave to add content coming from an external xml to nodes. One of the contents is an image. I'd like to download the image and save it to my drupal files directory if i don't have any other file with the same uri, if instead i already have a file with that uri, i'd like to rename it.

//get the image from the xml
$img = $qp->top()->find("Product")->attr("Pic500x500");
$filename = explode("/",$img);
$filename = array_pop($filename);
$filepath = "product_images/".$filename;
$uri = file_build_uri($filepath);
//SOMETHING TO CHECK IF I HAVE A DRUPAL FILE WITH THAT URI
if($check = "ok"){
  @copy($img, $uri);
}else{
  //download the file to temp, rename it and put it in my public dir
}

thanks!

pviquez@pabloviquez.com’s picture

$uri = 'public://file.xyz';

// Take a look at: file.inc::file_load_multiple
$files = file_load_multiple(array(), array('uri' => $uri));
$file = reset($files); // If empty, $file will be false, otherwise will contain the required file
elephant.jim’s picture

On the off chance anyone lands here looking for a way to lookup a file by name, see the first comment on https://api.drupal.org/api/drupal/includes%21file.inc/function/file_load... for an example how to do this with EntityFieldQuery.

voleger’s picture

If you looking for D8 solution:

/* @var \Drupal\file\FileInterface[] $files */
$files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $uri]);
ericclaeren’s picture

Thanks, this works!

jkamizato’s picture

Thanks voleger!

bcweaver’s picture

Thanks!  This was the answer I personally needed.  If this were stackexchange I'd upvote your comment.

voleger’s picture

bcweaver’s picture

Okay, I upvoted it

arifj’s picture

Thanks voleger, this was just what I was looking for. For anyone else who wants to lookup files by filename just swap out 'uri' for 'filename'!

CreateSmart’s picture

What about if you don't know the `'public://file.xyz';`.

For example an image from https://drupal.stackexchange.com/sites/default/files/blog-images/stack.jpg and we want to get the Id. We could do it like this:

        function getFileData($FileUrl) {
        $FileName=drupal_basename($FileUrl);
        $TargetFile = file_load_multiple(array(), array('filename' => $FileName));
        $FileData= reset($TargetFile);
        if($FileData):
        return($FileData);
        else:
        return FALSE;
        endif;
        }
        
        //Usage
        
        $FileUrl="https://drupal.stackexchange.com/sites/default/files/blog-images/stack.jpg";
        $FileData=getFileData($FileUrl);
        //::::file id
        $TheFiD=$FileData->fid;
        
        //::::file uri
        $TheUri=$FileData->uri;
       //Should print 'public://blog-images/stack.jpg' or 'private://blog-images/stack.jpg'
    
        //Upload userId
        //$UploadUserId=$FileData->uid;