1

I'm trying to do some custom stuff with a file upload field via the form API. I call to file_save_upload() but it thinks that the upload is empty because it's examining $_FILES['files'] but for some reason, my file is actually in $_FILES['CSV_file'] even when the file I uploaded is not a csv.

How might files superglobal end up with a CSV_file array key?

UPDATE

So apparently the form array

$form['my_menu_csv'] = array(
  '#name' => 'CSV files',
  '#type' => 'file',
  '#title' => t('Choose a CSV file'),
  '#title_display' => 'invisible',
  '#size' => 22,
  '#theme_wrappers' => array(),
  '#weight' => -10,
);

was causing the field to have the following HTML:

<input type="file" id="edit-my-menu-csv" name="CSV file" size="22" class="form-file">

and apparently the files array goes like $_FILES[$name], where $name = whatever the name attribute is in the HTML element. Makes sense! What doesn't make sense is why file_save_upload() expects to only ever encounter $_FILES['files'] but the form API uses a custom field name. Is this a bug in the form API?

1

I don't think it's a bug.

$_FILES['files'] is hard-coded into file_save_upload().

I believe you need to change how you construct your #name property. The usage example in the Form API does it like this:

'#name' => 'files[' . implode('_', $element['#parents']) . ']',

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.