Skip to main content

Example code to build an upload form in Drupal

An article from ComputerMinds - Building with Drupal in the UK since 2005
27th Mar 2007

Mike Dixon

Senior Mind
Hey, you seem to look at this article a lot! Why not Bookmark this article so you can find it easily in the future?

Putting together an upload form in Drupal 5 is easier than you think - there are just a couple of things you need to remember ...

The form is built like this:


function build_upload_form(){
    $form['#attributes'] = array('enctype' => "multipart/form-data");
    $form['file_upload']=array('#type'=>'file','#title'=>'Filename');
    $form['submit']=array('#type'=>'submit','#value'=>'Upload file');
    return $form;
}

And the submit is handled like so, note - the value passed into file_check_upload is the name (terminal array key) of the form element defined in your form building function ...


function upload_form_submit() {
    $file = file_check_upload('file_upload');
    //handle the file, using file_save_upload, or something similar
    if ($file){
    $file = file_save_upload($file,'some_path');
    }
}

And thats it - all the file details will be in the object $file and you can do with it whatever you like ...

Hi, thanks for reading

ComputerMinds are the UK’s Drupal specialists with offices in Bristol and Coventry. We offer a range of Drupal services including Consultancy, Development, Training and Support. Whatever your Drupal problem, we can help.