views:

562

answers:

3

I'm trying to extend a form and add an upload file field from within a module, I can see the file field just fine, but it's empty when I submit the form, the enctype is set.

  $form['#attributes'] = array(
    'enctype' => "multipart/form-data"
  );

  $form['file_upload'] = array(
    '#type' => 'file',
    '#title' => 'Attach Image'
  );

custom form submit hook:

$form['#submit'][] = 'user_images_handler';

is being called, but when I dump the form, the file field is empty, and when I try to access it, it's empty as well.

A: 

File uploads are special in that the 'submitted' (uploaded) data does not end up in the form, but needs to be processed separately (uploading is not really a part of form submission but a separate transmission process).

See the docs for file_save_upload(), and as an example, see how it is used on form submission usage from within the upload module.

Basically, you just try to save the upload by calling file_save_upload() with the name of the upload field (and some other arguments) and check the result of this try.

Henrik Opel
A: 

Hmm, I tried that and it didn't work...it still says the upload field is empty. I have a debugging print statement at the submit function but it doesn't print so I don't think it is even getting to the submit function.

Where are you suppose to put the file_save_upload() call?

Note, my form is named "mmil_upload_form ( $form_state )" and the submit function is named "function mmil_upload_form_submit ( $form, &$form_state )"

Mona
A: 

May be this could help: http://www.imedstudios.com/labs/node/22

related questions