Drupal 7 - Handling file uploads and moving files to the public file system

rahul's picture

This was the most time consuming aspect for the recent port of my Document module for Drupal 7.

You would be knowing that File system API underwent a complete revamp in Drupal 7 (see this and this link, a very good introduction of the changes and new concepts is available on Randy Fay's blog). In Drupal 6, handling file uploads was this easy:


{syntaxhighlighter brush: as3;fontsize: 100; first-line: 1; }$file = file_save_upload('file_form_field_name', array(), 'relative_path_to_files_folder'); if ($file !== 0) { //Set the status of the uploaded file. file_set_status($file, FILE_STATUS_PERMANENT); }{/syntaxhighlighter}

Drupal 7 has completely overhauled the File API introducing things like stream wrappers and managed/unmanaged apis etc. You would appreciate the architecture behind these changes in D7 once you understand how it works. But till you do so, it might really get onto you to get it working. And this is what happened to me during the D7 port of the Document module.

I made the following mistakes during the path (which you should avoid obviously):

  1. The first mistake I made was not to pass the stream wrapper's scheme to the file_save_upload method. In D7 and using managed File API, you need to pass in the stream wrapper's scheme compulsarily. I originally tried with this:

    $file = file_save_upload('document_filepath', array(), 'docs', FILE_EXISTS_RENAME);

    Here "docs" is a sub-folder inside the files folder to which I wanted to save the uploaded file to. Because I wanted to save to the public file-system path, I should have used the "public://" scheme as belows:

    $file = file_save_upload('document_filepath', array(), 'public://docs'), FILE_EXISTS_RENAME);
  2. The base path of the file-system you are trying to save the file to should not be passed to the file_save_upload method. Suppose your public file-system's path is "sites/default/files". And you want to save the uploaded file to "sites/default/files/docs". Then this is the call you should make:

    $file = file_save_upload('document_filepath', array(), 'public://docs'), FILE_EXISTS_RENAME);

    while I originally tried with this (which was wrong):

    $file = file_save_upload('document_filepath', array(), 'public://sites/default/files/docs'), FILE_EXISTS_RENAME);
  3. Finally the uploaded file's path in the $file object is now represented by:
    in Drupal 7 as opposed to:
    in Drupal 6.

So, here's the complete code from the Document module to handle a file upload and save it to the public file-system path:


{syntaxhighlighter brush: php;fontsize: 100; first-line: 1; }$file = file_save_upload('document_filepath', array(), 'public://' . variable_get('document_path', ''), FILE_EXISTS_RENAME); if ($file) { $fid = $file->fid; $doc_url = file_create_url($file->uri); //Set the status of the uploaded file. $file->status = FILE_STATUS_PERMANENT; file_save($file); } else { drupal_set_message(t('Please select a document to upload.'), 'error'); }{/syntaxhighlighter}





  I am unable to upload zif files...when I try to upload ,a wraning  "The specified file sample.zip 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." is shown...

Kindly give me a solution ...I tried configuring manage fields etc..but nothing worked..

By narendra (not verified)

You need to edit the file field of your content type. Under the field's label and helptext you will find the "allowed extensions", where you can add "zip".

By Wouter (not verified)

  Actually I have copied a module called word2book compatible with drupal 6 to drupal 7 ..In which I am uploading a zip file consist of filtered html files and this module will convert those into books...I have done some changes in .module file like pathe change public:// ,clone() etc  as a result  title is getting posted but not the content when I view it under content menu only tule is shown not the body of it...Kindly give me a solution ..

By narendra (not verified)
rahul's picture

Hi narendra, I do not see any module called word2book on drupal.org, so most probably you have a custom or proprietary module with you. I would not be able to help here.

By rahul

Can you please tell me equivelent function for  node_teaser( ) of drupal 6 in drupal 7

By Narendra Shanbhag (not verified)
rahul's picture


You might also be interested in the migarion guide.

By rahul

Thankx Rahul for your support...

Basically I am uploading zip file containing filtered html files and my module will convert in  to book.. Now I am able to display Index and  headings of book,pages  that I am uploading but..not able to view the body ...

I tried following code for assigning values to node->body

$child_node->body = array(
    'und' => array(
        'value'  => $body,
        'summary' => text_summary($body),
        'format' => '1',
        'safe_value' => $body,
        'safe_summary' => text_summary($body),
// othe code related to reirect and all   


But the thing is book shows index and on clicking on perticular page it rediredcts ..in the redirected page I am able to view Tiltle ,Next page prev page ,up but not body ...can you pls sugeest me anything which I need to take care off

By Narendra Shanbhag (not verified)
rahul's picture

Please see the documentation for node_object_prepare method Call that method and print_r its return value, that would help you. I am planning to write a blog post on this aspect, that might help you. I will try to write that blog post soon (maybe by tomorrow).

By rahul

i have created an object for stdClass ...but its showing a notice Undefined property: stdClass::$book in word2book_module_upload_form_submit() (line 319 of /var/www/drupal7/sites/all/modules/word2book/word2book.module) ...in this line   I have condition         if($node->book['bid'] == 0 || !isset($node->book['bid'])){

$node->book['bid'] = $node->nid;


In drupal 7 how ton get rid of this notice without turning off the drupal  messages ...Kindly explain thanks

By Narendra Shanbhag (not verified)
rahul's picture

Hi Narendra, I have already said that I would not be able to help on proprietary modules not available off d.o. Also please take care to keep your comments related to the blog post. I would remove any further comment that does not relate specifically to the blog post where you are making it.

By rahul

Add new comment