I've made a form. Now I want it to upload files. I've made a form element to do that:

  $form['attachments'] = array(
    '#type' => 'file',
    '#title' => t('Attachments'),
    '#name' => 'attachments[]',
    '#attributes' => array('multiple' => ''),
  );

I've set #name and #attributes.multiple because I want to use the HTML5 multiple files upload thingie. They invented that for a very good reason. I assume Drupal knows how to handle that.

How do I get the files in the submit handler? I don't want to save them to the file system. I want to e-mail them.

Ofcourse (?) I can just query $_FILES, but even I think that's not right in a framework like Drupal.

Is there a better way?

  • I don't think [form api file element type][1] supports that as of now, there is a html5 core initiative for D8 and hopefully we will see it change in D8. For now, you can try [plupload element type][2] (Abandoned project :( ) which uses the plupload js library by default, maybe you can take your hint from this module. [1]: api.drupal.org/api/drupal/… [2]: drupal.org/project/plupload – Dipen Jan 16 '12 at 19:45
up vote 0 down vote accepted

I haven't tried HTML5 Upload yet, but it might help you out.

  • 1
    That's only for managed files. And only in entity forms. Close enough though. And very relevant to my new project. – Rudie Jan 10 '13 at 22:10

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.