8

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.

1 Answer 1

13

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.

Sign up to request clarification or add additional context in comments.

4 Comments

yeah, I have with that no difficulty. However, once the file is uploaded, how do you save it, and use it in future functions?
after edit: ok, and now if I wanted to, say, display the file's values on the page?
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.
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.