By jeditdog on
I'm struggling to get a file to upload in Drupal 7.12. I can see in phpinfo(32) that the file did upload, but the tutorial I've been looking at isn't actually uploading. I don't need this to be very robust. What am I doing wrong?
I've been following:
http://api.drupal.org/api/examples/form_example%21form_example_tutorial....
http://api.drupal.org/api/examples/form_example%21form_example_tutorial....
My code is below. Any suggestions on how I can get this working?
function ts_menu(){
$items['ts_form'] = array(
'title' => 'File Test',
'page callback' => 'ts_file',
'access arguments' => array('send location'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ts_file()
{
//return 'get ready';
$h = array();
return drupal_build_form('file_test_form', $h);
}
function file_test_form($form_state) { // from
// If you are familiar with how browsers handle files, you know that
// enctype="multipart/form-data" is required. Drupal takes care of that, so
// you don't need to include it yourself.
$form['file'] = array( // From: http://api.drupal.org/api/examples/form_example%21form_example_tutorial.inc/function/form_example_tutorial_10/7
'#type' => 'file',
'#title' => t('Image'),
'#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function file_test_form_submit($form, &$form_state) { // Form http://api.drupal.org/api/examples/form_example%21form_example_tutorial.inc/function/form_example_tutorial_10_submit/7
$file = file_load($form_state['values']['file']); // Try 1
print_r($file);
$file = $form_state['storage']['file']; // Try 2
// We are done with the file, remove it from storage.
unset($form_state['storage']['file']);
// Make the storage of the file permanent
$file->status = FILE_STATUS_PERMANENT;
// Save file status.
//file_save($file);
// Set a response to the user.
drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $file->filename)));
//print_r($form_state);
exit();
}
Comments
Have a look at
Have a look at http://drupal.org/node/85922
that example still confuses
that example still confuses me because it's Drupal 6 and some of the functions used have been removed or deprecated.
Does anyone have a quick example on how to handle file uploads? Or a lengthy one? I realize there is a lot to understand, but the documentation is pretty much non existent. I'm surprised it's this difficult considering it's fairly easy in Php itself.
I learned how to work with
I learned how to work with Drupal's file API using the tutorial named 'Working with Files and the File API' which can be purchased here: http://buildamodule.com/
I found it very helpful, it when through both managed and unmanaged files, private and public files, and explained how the stream wrappers work allowing for the public:// and private:// schemas.
Contact me to contract me for D7 -> D10/11 migrations.
Righto. Because I gave you a
Righto. Because I gave you a bum steer, here is a free working example:
and yes, this originally took me a while to figure out as well...
To summarize what happened
To summarize what happened here. I was able to get the examples working but only in a Linux environment. My windows dev environment was at the heart of the problem.
I'm sure the main problem was with my Apache Setup so if you're having this problem, I suggest trying it on Linux.