4

I have a form with a file upload widget:

$form['myFile'] = array(
    '#tree' => false,
    '#type' => 'file',
    '#title' => t('Choose a file')
);

In the form validation handler, I upload the file and validate it with:

$validators = array(
    "file_validate_extensions" => array(variable_get("allowed_extensions")),
    "file_validate_size" => array(variable_get("max_image_size"))
);
$file = file_save_upload('myFile', $validators);
$form_state['file_storage']['myFile'] = $file;

Then, in the submit handler, I save the file using:

$file = $form_state['file_storage']['myFile'];
file_save($file);

This seems to work fine as I can find an entry for the file in the file_managed table:

+-----+-----+--------------+-----------------------------+------------+----------+--------+------------+
| fid | uid | filename     | uri                         | filemime   | filesize | status | timestamp  |
+-----+-----+--------------+-----------------------------+------------+----------+--------+------------+
|  49 |   1 | download.jpg | temporary://download.jpg    | image/jpeg |     5410 |      0 | 1350312114 |
+-----+-----+--------------+-----------------------------+------------+----------+--------+------------+

The problem is that I can't find the file anywhere on disk:

$ su -
Password:
# updatedb
# locate download.jpg
/home/me/Pictures/download.jpg

As you can see, the only download.jpg found is the one in my pictures folder which I passed into the form. I can't find anything on the server (in /var/www/). So where is the file? Is it a BLOB in the drupal database? Has it simply not been uploaded properly? If so, what did I do wrong?

5

You're just missing the third parameter for file_save_upload():

$destination: A string containing the URI $source should be copied to. This must be a stream wrapper URI. If this value is omitted, Drupal's temporary files scheme will be used ("temporary://").

The uri in your table is temporary://download.jpg which seems to support that.

To save your file in the public file system you just need to pass that parameter in:

$destination = 'public://some/folder/';
$file = file_save_upload('myFile', $validators, $destination);
1
  • 1
    Right you are. For the record, I used public://images, but I had to create the folder myself and change the user for it to work: cd /var/www/drupal/sites/default/files/; mkdir images; chown apache images
    – Shawn
    Oct 15 '12 at 18:19

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.