5

I've got a drupal form with a fieldset (actually, two). Inside the fieldset, there are a number of dynamically generated file fields. Unfortunately, I can't seem to access these fields' contents from file_save_upload. Currently, I'm attempting to use the approach described here to set the #name property of my file fields so that the files array gets built correctly. However, file_save_upload always returns 0. What am I doing wrong?

Form (this is an example, since the actual form is dynamically generated by a bunch of code that isn't relevant here):

$form['release_data_graphics'] = array(              //This has a few other random things in it
    '#type' => 'fieldset',
    '#title' => 'Brand Release Graphics',
    '#collapsed' => FALSE,
    '#collapsible' => TRUE,
    '#weight' => 11,
);
$form['release_data_graphics']['graphics'] = array(  //This has a few other random things in it
    'folders' => array(                              //This is here because I iterate through it
        'folder1' => array(
            '#type' => 'fieldset',
            '#name' => 'Folder 1',
            '#description' => 'some description',
            'graphics' => array(                     //This is here because I iterate through it
                'graphic1' => array(
                    '#type' => 'file',
                    '#title' => 'Upload graphic 1',
                    '#name' => 'files[1]',
                ),
                'graphic2' => array(
                    '#type' => 'file',
                    '#title' => 'Upload graphic 2',
                    '#name' => 'files[2]',
                ),
            ),
        ),
    ),
);

Then, in my submit handler, I iterate through the folders and child graphics arrays (which I won't show here) with file_save_upload. My code ends up running file_save_upload like this:

graphic1 = file_save_upload('1', file_directory_path(), FILE_EXISTS_REPLACE);
graphic2 = file_save_upload('2', file_directory_path(), FILE_EXISTS_REPLACE);

When I select a few items for upload and then print the files table in the submit callback, the files table looks good:

[files] => Array(
    [1] => test1.png
    [2] => test2.png
);

So I'm really having trouble figuring out what's going on...

5
  • How are you dynamically generating file fields. Are you doing it via AHAH? If not so then it can be the reason of problem. Drupal would not allow to accept dynamically created form fields and it would generate a validation error."An illegal choice has been detected" Jun 29, 2011 at 0:32
  • Nope, it's all being done with the standard core Forms API. Basically, I loop through the results of a db query for images that the user can upload and add nodes to the form that are similar to 'graphics1' and 'graphics2'. Nothing fancy.
    – QRohlf
    Jun 29, 2011 at 16:02
  • Upon further debugging, it looks like the ['#post']['files'] array is being generated properly, but that $_FILES['files'] isn't, and so file_save_upload can't find the file. It looks like I'll have to ditch the fieldset if no one has a workaround.
    – QRohlf
    Jun 29, 2011 at 20:29
  • I think it is not recommended to use $_FILES array. Are you getting some kind of fid in ['#psot']['files']? If so then your files record are being dumped in 'upload' database table. Jun 29, 2011 at 20:38
  • I'm not getting any fid entries in [#post][files], just the [name] => filename.png output being shown above.
    – QRohlf
    Jun 30, 2011 at 22:37

2 Answers 2

1
+50

Add this to beginning of your form

You need this:

$form['#attributes'] = array('enctype' => 'multipart/form-data');

Also:

file_save_upload($source, $validators = array(), $dest = FALSE, $replace = FILE_EXISTS_RENAME)

you have

graphic1 = file_save_upload('graphic1', file_directory_path(), FILE_EXISTS_REPLACE);

you should have

$graphic1 = file_save_upload('graphic1',,file_directory_path(), FILE_EXISTS_REPLACE);

One thing I've noticed in D6 with file uploads is that using only file_save_upload() will result in your file being purged on a subsequent drupal cron run. This happens because all files are uploaded in Drupal with a status of 0.

Therefore, you should be calling your hook_submit more like this...

// Save new file uploads.
if ($file = file_save_upload('file_upload', $validators, file_directory_path())) {

// Do something with $file here.

//Make file permanent
file_set_status($file, FILE_STATUS_PERMANENT);
}
1
  • Thanks for the answer - I ended up solving the problem by removing the top level fieldset and rearranging my UI so that there was only one upload per fieldset. It actually ended up making things a lot easier to deal with in the form submission.
    – QRohlf
    Jul 7, 2011 at 18:56
1

There is a known bug when file upload inputs are used inside fieldset in the Drupal 6 API. See these for more info:

structure of $_FILES when more than one file input is used:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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