I have a really simple form with a file upload. It actually used to work until I recently noticed it's not saving the file. When I check for the submitted values, the file field is empty, but the file is in $_FILES.
Here is how I build the form:
function tag_graphics_form($form_state) {
$form = array();
$form['tag'] = array(
'#required' => TRUE,
'#type' => 'textfield',
'#title' => t('Tag'),
'#size' => 30,
);
$form['image'] = array(
'#type' => 'file',
'#title' => t('Image for tag page'),
'#description' => 'Recommended width for image: 605px'
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Page Description for SEO')
);
$form['#attributes'] = array('enctype' => "multipart/form-data");
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save'
);
return $form;
}
And this is how I process things in the submit:
function tag_graphics_form_submit( $form, &$form_state ) {
// save the image and put to the database
print_r($form_state['values']); // the image field is completely empty here. other fields are ok
print_r($_FILES); // the uploaded file is here just fine
if ($form_state['values']['image']) { // all of this used to work...
$validators = array();
$file = file_save_upload('image', $validators, file_directory_path());
file_set_status($file, FILE_STATUS_PERMANENT);
$filepath = $file->filepath;
}
// more code...
}