Topic: Drupal File Upload with hook_form_alter()

Hello, Godbit!

I've been "lurking" around here for quite some time but never signed up or joined in on any of the discussions.  I really appreciate this site and the community, though, even if I haven't actively participated until now.

I was wondering if someone could help me out.  For the past couple of years, I have been a .NET developer by trade.  Prior to that, programming was a hobby and PHP was the tool of choice.  Recently, a friend of mine asked if I could help him out with some Drupal work he needs done for a site he is running.  I've been working on this for awhile now (longer than it would take an experienced Drupal programmer), we're both very anxious for this to be done - timelines, you know...So, I'm hoping someone here might be experienced enough to help me with what I'm doing wrong.  I feel silly, as this type of thing should be so easy...

All I want to do at this point is add a file upload to a form in another module.  I've created the file upload control to the form using the hook_form_alter() method, but using the Drupal way of working with files does not produce a $file object.  If I use the standard PHP $_FILES array, I have access to the file.  I'm not always very good at explaining things, so let me provide you with the code.

case 'uc_attribute_option_form':
  if (!user_access('administer option images')){
  $form['option_image'] = array(
  '#type' => 'file',
  '#title' => t('Image'),
  '#name' => t('option_image'),
  '#description' => t('Add an image to this option'),
  '#size' => 47,
  '#weight' => 2,
  // '#default_value' => '',
  $form['option_image_test'] = array(
  '#type' => 'textfield',
  '#title' => t('Testing'),
  '#description' => t('Just testing the alter_form() hook for uc_attribute...'),
  '#name' => 'option_image_test',
  '#weight' => 3,
  // '#default_value' => 'Default test value',
  $form['#attributes'] = array('enctype' => 'multipart/form-data');
  $form['#submit']['uc_option_image_uc_attribute_option_form'] = array();

The textfield in the code is just for debugging purposes.  I wanted to make sure I could grab the value from something I added to the form via hook_form_alter().  Inside the uc_option_image_uc_attribute_option_form() method referenced in the "submit", I have just some basic debug right now...


This code produces the following when the submit button is clicked:

Array ( [aid] => 10 [oid] => 157 [name] => Blue Bayou [ordering] => 0 [cost] => 0.00 [price] => 0.00 [weight] => 0 [op] => Submit [submit] => Submit [form_token] => 9780d12672a934d2e57d42e6bf472306 [form_id] => uc_attribute_option_form [option_image] => [option_image_test] => file testing )

Array ( [name] => testBlueGreen.jpg [type] => image/jpeg [tmp_name] => /tmp/php959q8d [error] => 0 [size] => 17399 )

As you can see, the $form_values array has the option_image element, but no value.  The $_FILES array does have the data, however.  After further research, I've also tried using Drupal's file api with $file = file_upload_check('option_image'), but it doesn't return anything either.

Is there anyone who can shed some light on this for me?

Thank you!

-- j o e l

Re: Drupal File Upload with hook_form_alter()

I couldn't figure this out, and no one on the Drupal forums had an answer either.  In the end, I just used straight PHP to resolve the file upload.  If anyone can think of a reason why this might have been, I'd be glad to hear it, in case I ever need to use Drupal again.


Re: Drupal File Upload with hook_form_alter()

I'm a bit late in seeing this. I'm not sure of all the details of the form you were trying to create, but I assume you've already looked into using the Upload Module that is already in Drupal Core? If so and it wasn't suitable, then perhaps you could look into using the FileField module ( I've never really messed around hook_form_alter(), so I'm afraid I won't be able to offer much help in troubleshooting your initial attempt.

Hope that helps.

Exercise your faith!