Please save me from ripping out the rest of my hair...

I've got a "managed_file" field in a form; nothing special. When the form is submitted, I run something like the following code to, presumably, catch and save it:

$destination = "public://video_posters";
$file_return = file_save_upload('field_poster_frame', array('file_validate_extensions' => array('jpg jpeg png gif')), $destination, FILE_EXISTS_RENAME);
if ($file_return) {
     $file_return->status = FILE_STATUS_PERMANENT;
     file_usage_add($file_return, 'my_module', 'node', $some_nid);

(There is more testing of $file_return to catch the other cases, and a few other things, but you get the idea.)

What's happening is:

  • An entry shows up in the file_managed table that is correct except that its uri is temporary://the_file_name.jpg instead of, presumably, public://video_posters/the_file_name.jpg.

  • The file was indeed uploaded and is in the indicated place in temp storage -- just not where I want it to be.

Am I missing something obvious? Any things to check on? This is really driving me over the edge. Thanks!

1 Answer 1


The managed_file element handles moving the file into place and adding a file record, you don't need to save it yourself. The file module does all this in a validation function which makes the fid available in the submit handler.

So, for example, if your file element is defined with $form['file_id'] = ..., you can change your code to something like the following

$fid = $form_state['values']['file_id'];
$file = file_load($fid);
$file->status = FILE_STATUS_PERMANENT;
file_usage_add($file, 'my_module', 'node', $some_nid);
  • Thanks for the note. I see what you're saying, but it may not apply to my situation: if my brain were not so scrambled, I would have remembered to mention that this file is not getting connected to the content type in whose form it appears, but a separate content type instance that I'm then hooking into the one created by the form. In any case, I tried putting your code into a _form_submit() handler where I could get at $form_values, and, while there is an apparent fid for my field, saving like you suggested still leaves the file_managed's uri as temporary://. Sigh. Other thoughts? :)
    – Jim Miller
    Commented Jul 11, 2013 at 0:50
  • 2
    Have you added an #upload_location property to the element in the form? That controls where the file is auto-saved to, so if you set that as public://whatever/ things should theoretically work
    – Clive
    Commented Jul 11, 2013 at 9:54
  • 1
    AHA! That (the #upload_location attribute, that is) did it. Blurgh; thanks very much!
    – Jim Miller
    Commented Jul 11, 2013 at 20:21

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.