I'm getting the values of the node that I'm about to create with my custom module and the function " hook_node_insert " but I'm not receiving the data of my image like the path and name.

I'm just getting the fid, is that useful?

Is there any other method to get the data?

Thanks guys !


Yes, the fid is a foreign key into Drupal's file_managed table. You can use the file_load() function to retrieve the file object which contains the name & path.

  • Thanks I'm trying to make it work but it returns nothing, should I have to use that function AFTER the node is save?. Or is it good in node_insert? – DarkteK May 25 '15 at 6:27
  • If you have a valid fid in your hook_node insert() function, you should be able to use file_load($my_fid) to retrieve the object. – Shawn Conn May 25 '15 at 6:35
  • @DarkteK hook_node_insert is called after a node has been saved. So, you should be able to load the file using its fid. – AjitS May 25 '15 at 8:15
  • @AjitS Weird that I can't get this function to work with the fid...still I managed to work it out :D still thx – DarkteK May 25 '15 at 19:51

Drupal gives this by single file load from database:

$objFile = file_load($fid);
$imgabspath = objFile->uri; //getting file page (Example of image file absolute path)
print file_create_url($imgabspath);  //This is your file absolute url
$filename = objFile->filename; //getting file name

Still you are not getting result proper then there is some problem into $fid so confirm it by:

$node = node_load($nid);
// assuming file field has been added to the node has field_example_file machine name.
if (!empty($node->field_example_file)) {
  // $img will contain an array, which has the data of a file (file object converted to array I suppose..)
  $img = $node->field_example_file['und'][0];
$fid = $img['fid'];//This is your fid...

NOTE : file_create_url() always creates an absolute URL, either using the global $base_path that you have defined, or using the one that Drupal guessed during bootstrap. Also note that file_load() doesn't return title and alt attributes of field_image.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.