I have a content type with an imagefield. I need to get a reference to the file object during node save or update, so I can do something with it.

function foo_node_presave($node){
    if ($node->type ==='foo'){
        $filename = $node->original->field_foo_image['und'][0]['filename'];

In the above code, $filename isn't populated yet when a new node is being created. It does work during a node update if there was already a file in that field.

How can I get a reference to the file object in a filefield/imagefield in a hook during node creation?

1 Answer 1


Even if the field hasn't been updated yet you should have access to the fid column (the file ID), which you can use in conjunction with the file_load() function to get the filename:

// Load a file object based on the fid column fo the field.
$file = file_load($node->field_foo_image[$node->language][0]['fid'];

// Get the filename.
$filename = $file->filename;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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