Howto: Drupal File Upload Form

22,926

This is not too difficult, you can see some info here. An example of a form with only a file upload.

function myform_form($form_state) {
    $form = array('#attributes' => array('enctype' => 'multipart/form-data'));
    $form['file'] = array(
        '#type' => 'file',
        '#title' => t('Upload video'),
        '#size' => 48,
        '#description' => t('Pick a video file to upload.'),
    );
    return $form;
}

EDIT:

Now to save the file use the file_save_upload function:

function myform_form_submit($form, $form_state) {
    $validators = array();
    $file = file_save_upload('file', $validators, 'path');
    file_set_status($file, FILE_STATUS_PERMANENT);
}

2nd EDIT:

There's a lot of questions and ways to do the things you described. I wont go to much into the actual code of how to handle a csv file. What I would suggest is that you use the file id to keep track of the file. That would enable you to make urls that take a fid and use that to load the file you want to work on. To get from your form to the next step, you can use the #redirect form property to get your users to the next step. From there is really depends how you do things, what you'll need to do.

Share:
22,926
Nick Heiner
Author by

Nick Heiner

JS enthusiast by day, horse mask enthusiast by night. Talks I've Done

Updated on July 09, 2022

Comments

  • Nick Heiner
    Nick Heiner almost 2 years

    I'm having difficulty figuring out how to write a module with a form that uploads files, in Drupal 6. Can anyone explain this, or point me to a good example/documentation discussing it?

    EDIT:

    Here is entirely what I am trying to do:

    1. User uploads a .csv
    2. Module reads the first line of the file to get fields
    3. User matches csv fields with db fields
    4. Each csv line is saved as a node (preview it first)

    So far, I can do 1, 2, and 4 successfully. But it's unclear exactly how the steps should interact with each other ($form_state['redirect']? how should that be used?), and what the best practices are. And for 3, should I save that as session data?

    How do I pass the file data between the various steps?

    I know that node_import exists, but it's never worked for me, and my bug requests go ignored.

    2nd EDIT: I used this at the start and end of every page that needed to deal with the file:

    $file = unserialize($_SESSION['file']);
    //alter $file object
    $_SESSION['file'] = serialize(file);
    

    I'm not sure it it's best practices, but it's been working.

  • Nick Heiner
    Nick Heiner over 14 years
    yeah, I have with that no difficulty. However, once the file is uploaded, how do you save it, and use it in future functions?
  • Nick Heiner
    Nick Heiner over 14 years
    after edit: ok, and now if I wanted to, say, display the file's values on the page?
  • googletorp
    googletorp over 14 years
    Can you be a bit more precise. Rendering an image, video or txt file will require very different approaches. What are you trying to do, that can't be done with CCK's file field? Anyways $file will be the file object, so you should be able to do whatever you want.
  • Hitesh
    Hitesh about 10 years
    @googletorp : I am newbie in drupal, if I have a registration form with multiple field along with upload button how do I achieve this. Since you are already declaring ` $form = array('#attributes' => array('enctype' => 'multipart/form-data'));` ? .... will it not affect other fields of form