By eugene2k on
Ok, either I'm doing something wrong, or Drupal has a crappy bug. According to the documentation the code below should save an uploaded file in a temporary directory. However it doesn't, the weirdest thing is that the $_FILES array is empty (or not defined) and my form does contain a file field.
function upload_form_submit($form, &$form_state) {
global $user;
print_r($_FILES); //this prints nothing!!!
if (file_save_upload('file') {
db_query("INSERT INTO {layouts} (uid, tid, name) VALUES (%d, %d, '%s')", $user->uid, $form_state['values']['type'], $form_state['values']['name']);
drupal_set_message('Your layout has been submitted.');
}
else {
drupal_set_message('Couldn\'t save file','error');
}
}
Comments
File upload in Drupal 6
If $_FILES is not set then your problem is with php/web server and not with Drupal. Either your function is not being called from your form result (which you could verify by dumping the stack in your function) or your web server did not recognize your form result as a valid file POST. You might want to look at $_POST global also and see what's in there, if the other fields from you form are in there.
I'm fighting with file
I'm fighting with file uploads too for the moment but I don't have the empty $_FILES problem. Did you set
in your $form?
Trogie
I didn't, thanks :)
I didn't, thanks :)
I am stuck at file upload
I am trying to figure out the file upload inside drupal 6 forms. ia can access inside validate hook but not the submit or form_submit. can some one please guide me about file uploading steps. Really have no idea where the things are getting wrong.
my source code:
function fileupload_form()
{
$form['#attributes'] = array('enctype' => "multipart/form-data");
$form['file']['cat_image'] = array(
'#type' => 'file',
'#title' => t('Some File'));
$form['file']['cat_banner'] = array(
'#type' => 'file',
'#title' => t('Some File'));
return $form;
}
function fileupload_validate($node, &$form)
{
$dir = drupal_get_path('module', 'fileupload') . '/files';
if(isset($_FILES['files']['name']['cat_image'])){
$name = $_FILES['files']['name']['cat_image'];
$size = $_FILES['files']['size']['cat_image'];
$type = $_FILES['files']['type']['cat_image'];
$file = file_save_upload($file, array() , $dir);
if($file){
drupal_set_message("You uploaded $name, it is $size bytes, and has a mimetype of $type.");
}
else{
drupal_set_message("file upload failure");
}
}
Are they large files ?
I saw this happen with large files. You might need to increase either php's limit or apaches
trogie -> Thank you thank you thank you!!!!!!!!!!!!!
trogie: Thank you.
You just saved my arse after two hours of bashing my brain on my keyboard!
:-)
me too :O you'd think their
me too :O
you'd think their would be some documentation with the file field in the FAPI api docs....
Apparently not O.o
RTFM!
For further support or projects contact me.
Source Control - Web Design, Development and Hosting Oxfordshire, UK