R – Drupal 6 hook_form_FORM_ID_alter adding upload file field

drupaldrupal-6

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.

Best Answer

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.

Related Topic