Drupal 7 – Handling file uploads

When trying to deal with custom forms with file upload fields, you may sometimes get a error regarding extensions:

The specified file “your file name here” could not be uploaded. Only files with the following extensions are allowed: “jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp”

To allow all extensions, you have to manually set the ‘file_validate_extensions’ validator to an empty array.

$validators = array('file_validate_extensions' => array());
$file = file_save_upload('upload', $validators);

For more information, check out Drupal API’s documentation at http://api.drupal.org/api/drupal/includes–file.inc/function/file_save_upload/7

The bare requirements for a file upload form is the “multipart/form-data” enctype, a “file” field, and the standard “submit” button. An example of a basic structure:

$form ['upload'] = array (
'#type' => 'file',
'#title' => 'Choose file',
'#description' => 'Upload a file.' );
$form ['submit'] = array ('#type' => 'submit', '#value' => t ( 'Submit' ) );
$form ['#attributes'] = array ('enctype' => "multipart/form-data" );

15 thoughts on “Drupal 7 – Handling file uploads

  1. David


    I’ve never worked with hooks or anything similar complex in Drupal. And I’m just migrating to Drupal 7. Can you please explain where I should put the required lines of code to allow all file extensions to work?

    Thanks a lot.

  2. ryan

    Hi David, these goes into where you handle the upload form submission (if you have a custom module doing that). You can try search your code for a ‘file_save_upload’.

    If you are not using a custom module then I think you can set them using the admin ui.

  3. David

    Hello Ryan. Thanks for the reply. I’m not using any custom module. But still can’t figure it out. I want to allow my authenticated users to upload files with any extension for a specific content type. In Drupal 7 when editing the Content Type, the File field requires me to set the allowed file extensions. Any ideas on how to bypass this (possibly using your above mentioned method)?

  4. ryan

    Hi David, seems like its not a trivial matter unless you’re comfortable with modifying core code. May I know where is your uploading happening? Is it a field in a custom content type or ?

  5. David

    Yes. What I’m trying to do is create an internal (within-the-site) file exchange system. Think YouSendIt.com. But our own custom version, allowing my clients and me to share project files easily.
    I’ve created my own content type which I called “FileShare”. Of course for security reasons this content type will be accessible only to a specific group of users. The content type has a File Field, which will allow these users to upload files to the server. Given that the projects I work on can have an unlimited list of possible extensions, instead of try to list them all, I’d rather say which ones NOT to accept or even just disable that validation all-together. (For this content type).
    I’d appreciate your help.

  6. Jonathan Puddle

    David, did you have any success with this? I need to do pretty much the same thing, and I’m stuck on how to make the content type available on my upload page. Yeah, I’m a Drupal newb.

  7. David

    I’m definitely not a Drupal “newb”, but sadly I’ve had no success with this so far. So any help from Ryan would be greatly appreciated. Temporarily we got the following subject going on the Drupal forums going:

  8. Justin

    You don’t need $form [‘#attributes’] = array (‘enctype’ => “multipart/form-data” ); in Drupal 7. It’s taken care of automatically when you have a file field in a form

  9. amazed

    How can we get file name uploaded with this?
    We can get other variable with

  10. Justin

    It will be in the $form_state array in the form’s validate and submit functions. Do a dpm($form_state) in one of those functions to see all the data.

  11. charitha

    i wrote module to upload image using form.it is working well and images are save in database.But i want to display uploaded images in home page in my site.how do this ….please help me…

    1. ryan

      Hi Charitha I can only point you in a direction: read up on drupal’s foundations and search for relevant modules. There are popular modules which takes care of uploading of images and inserting them into nodes, you don’t really have to code one yourself.


Leave a Reply

Your email address will not be published. Required fields are marked *