Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto: Drupal File Upload Form

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.

like image 854
Nick Heiner Avatar asked Aug 10 '09 04:08

Nick Heiner


1 Answers

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.

like image 86
googletorp Avatar answered Sep 20 '22 15:09

googletorp