I want a really simple upload because I need to import some data to populate a table. So I have this:

function product_import_form() {

	$form['#attributes'] = array('enctype' => "multipart/form-data") ;
	$form['upload'] = array(
		'#type' => 'file'
		) ;
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('Import'),
		'#weight' => 10
		);

	return $form ;
}

which successfully displays the form. I find a file and click 'import'. And I go to this validation routine:

function product_import_form_validate($form_id, $form_values) {
drupal_set_message(hackory_var($form_values)) ;
}

The "hackory_var" is my own routine to return a "var_dump" as a string, and the result is this:

Array
(
    [upload] => 
    [op] => Import
    [submit] => Import
    [form_token] => 96fd82d5062217094045e57118b94109
    [form_id] => product_import_form
)

As you can see 'upload' is blank, doesn't matter what file I specify.

Any idea why this isn't working? Have I missed something obvious?

Steve

Comments

SteveTurnbull-1’s picture

For completeness, here's the HTML that's generated:

<form action="/import/products"  method="post" id="product-import-form" enctype="multipart/form-data">
<div>
<div class="form-item">
 <input type="file" name="files[upload]"  class="form-file" id="edit-upload" size="60" />
</div>

<input type="hidden" name="form_token" id="edit-product-import-form-form-token" value="96fd82d5062217094045e57118b94109"  />
<input type="hidden" name="form_id" id="edit-product-import-form" value="product_import_form"  />
<input type="submit" name="op" id="edit-submit" value="Import"  class="form-submit" />

</div>
</form>
skywalker2208’s picture

The way I got it to work was in the hook_validate I used file_check_upload(); Inside the function you would put 'upload'. Here is an example for something I did.

   $form['#attributes']['enctype'] = 'multipart/form-data';
  $form['user_offline_image'] = array(
    '#type' => 'file',
    '#title' => t('Upload a user offline image'),
    '#description' => t('Image that is displayed next to the users name when they are offline. Files allowed: .gif, .png and .jpg'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

In my hook validate

    if ($online_file = file_check_upload('user_online_image')) {
      print_r($online_file);  //if you do a print_r you should see the filename and among other things

    }

I hope this helps you out.

SteveTurnbull-1’s picture

Thanks.

The problem was that I couldn't believe the way to access the file information was to use file_check_upload with the field name used in the form. I was being too clever for my own good.

I suppose I'm still kind-of going "huh?" but I do have it working now. Thanks for convincing me.

Steve