Community & Support

Forms api doesn't upload file in custom module, not a node

I'm trying to upload a file using the new forms api. I am able to build the form, but when I call the variable up in my _submit function, it only has the text name of the file. Also, there $_FILES variable is totally empty. When I add a file upload form item in a module using node hooks, it uploads fine. Perhaps the files only attach when using node hooks? Here's my form:

function my_form() {
  $form['image_upload'] = array('#type' => 'file', '#title' => 'Image', '#size' => 40);
  $form['submit'] = array('#type' => 'submit', '#value' => 'Submit');
  return drupal_get_form('my_form', $form);
}

And my submit function:

function my_form_submit($form_id, $form_values) {
  // this should have the uploaded image, right?
  $file = $form_values['image_upload'];
  // or atleast this should have the file info
  $file = $_FILES['image_upload'];
}

Is there something special I need to do in either the form, or the _validate/_submit functions to retrieve the uploaded file? Any help is greatly appreciated! :)

Comments

file_save_upload

I'm having some similar problems, but have had good luck using file_save_upload. In your case, you might try:

$file = file_save_upload('image_upload');

------
Personal: Outlandish Josh
Professional: Trellon

------
Personal: Outlandish Josh
Professional: Chapter Three

I found what I needed

It was this little gem:

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