This question extends a prior question. I the previous one, I found several errors relating to coding, and thought I had solved the basic question, only t discover that it still is not working correctly.

I have a form to get a file name for opening (http://pastebin.com/8NfGGNSn). When I set the form field type as a textfield, the form widget saves my text entry (in the $form_state ['values']['upload_file] field). When I change the form widget type to 'file' the entry text from the form is not saved. My browser is Firefox and it makes available a browse button and a file selection window in the form. When I select the file from the window dialog, the form widget is completed with the full file path, however, it appears that the path text in the widget is not being placed in $form_state[values][update_file]. It remains empty. IN both textfield and file cases I am checking the value of the $form_state array during validation, so there should not be any clear or reset activity at that point. Any ideas why the content is deleted from the widget? OR does a file name get saved at another location?

2 Answers 2


File input fields work differently than other input fields. This is a restriction of HTML. The file path is not transmitted to the server, because it is considered sensitive information. Only the file data and the name of the file is transmitted to the server.

Use file_save_upload() to save the file. To find out what to pass as the $source argument, print out both $form_state[values] as well as the $_FILES array.

  • I had not thought of the web limitations. I am trying to open a file for reading using PHP to import content to create nodes. In file_save_upload($source, $validators, destination, $replace) how do I get the value from the form (#type = 'file') to $source?
    – Ashlar
    Commented Dec 15, 2011 at 22:41
  • I do not know what should be passed as $source argument, but have a look at my updated answer.
    – Oswald
    Commented Dec 15, 2011 at 22:51

Oswald is right: You need to use file_save_upload() to save the uploaded file. The first argument of the function is the string that identifies the file form field in the form array.

If you look at the code of locale_translate_import_form(), and you compare it with locale_translate_import_form-submit(), you will notice that the first uses the following code:

  $form['import'] = array(
    '#type' => 'fieldset', 
    '#title' => t('Import translation'),
  $form['import']['file'] = array(
    '#type' => 'file', 
    '#title' => t('Language file'), 
    '#size' => 50, 
    '#description' => t('A Gettext Portable Object (<em>.po</em>) file.'),

The submission handler uses the following code:

  // Ensure we have the file uploaded
  if ($file = file_save_upload('file', $validators)) {
    // …

Similar code is present in aggregator_form_opml(), and aggregator_form_opml_submit().

  $form['upload'] = array(
    '#type' => 'file', 
    '#title' => t('OPML File'), 
    '#description' => t('Upload an OPML file containing a list of feeds to be imported.'),
  if ($file = file_save_upload('upload', $validators)) {
    $data = file_get_contents($file->uri);
  • Thanks, I'll give the check to Oswald, and pass some up arrows on to you soon. I seem to find your super answers wherever I go around here! If you are not writing a book on Drupal, you should, your command of the material and writing style are top notch.
    – Ashlar
    Commented Dec 16, 2011 at 3:33

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.