Hello and thanks for looking.

I'm doing some development work for a friend and have been run into a problem I can't seem to wrap my head around. I feel silly because it seems as though this is a simple enough task, but I can't get it to work.

I am trying to use hook_form_alter() to add a file upload to a form on an existing module. I've been able to successfully add it to the page using the following:

case 'uc_attribute_option_form':
	  if (!user_access('administer option images')){
	  	break;
	  }
	  
	  $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();
	  break;

In the submit I have written in my module, I have the following minimal code for debug purposes:

        print_r($form_values);
        print_r($_FILES["option_image"]);

The submit code outputs the following:

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 shows an empty value for 'option_image'. However, if I use the globabl PHP $_FILES array, it locates the 'option_image' field just fine.

What am I missing? What is the appropriate (Drupal) way to load the Drupal $file object with the 'option_image' file? Please forgive my ignorance on this matter, as I'm sure this has been covered before. I've looked over various posts, and even in some Drupal books, but I haven't seen anything which covers the scenario described here.

Thanks for your help!

Comments

comterkumar’s picture

The following code is working for me

function image_uplad_form()
{
$form['#attributes'] = array('enctype' => "multipart/form-data");
$form['file_upload']=array('#type'=>'file','#title'=>'Filename');
$form['submit']=array('#type'=>'submit','#value'=>'Upload file');
return $form;
}
function image_uplad_form_submit($form_id, $form_values)
{
$file = file_check_upload('file_upload');
//handle the file, using file_save_upload, or something similar
if ($file)
{
$file = file_save_upload($file,'files/images');
}
else
{
form_set_error('file_upload', t('Browse a file to Upload.'));
}

$unixtime = time();
$query3=db_query("INSERT INTO {user_images} (date,image_path) VALUES (%d,'%s')",$unixtime,$file->filepath);

}

j o e l’s picture

Thank you, vbkumar.

That still doesn't work for me. In fact, if I copy just the first line...

$file = file_check_upload('file_upload');

...nothing is returned in the $file object. I noticed your example is a standard form. I'm trying to add the file upload using the alter form hook. Is there anything special I need to do because of this?

j o e l’s picture

Does anyone have a working example for this when using it with the form alter hook?

Thank you.