1

long time reader, with a small question on my hands.

I have been working on adding multiple file upload support to a CakePHP 2.x project, I've implemented as such in a view here;

    <label>Supporting Document(s):</label>
    <?php                   
        echo $this->Form->file('files.', ['type' => 'file', 'multiple' => ($allowMultipleSetting ? 'multiple' : false)]);
    ?>

But I'm a little confused, what is the purpose of the '.' in 'files.' here? I can see what it can do, it gives me an array of file objects in my controller when I do a $this->data['files'], which is desired, and without it, I just have the one file object, even with multiple set to true from option parameters passed to this view.

Does anyone know what 'magic' or Cake 'thing' happens here so that, when a dot/full stop is added to the end of the key name, Cake shows all the images selected?

Thanks!

1 Answers1

2

In Cake typically the first parameter of the FormHelper you are using is for the column name it is using in the database. So it could be Model.column_name for an example, or just coulmn_name as the parameter.

As long as you make sure the column name matches up there you are good. Cake will take care of the rest and output the <input type="file" /> with the correct name attributes.

The "." you are referencing is because that is setup for multiple file uploads, and if it is giving you problems you can remove it and specify the columns in the options. Such as: "'name' => 'data[Model][fieldname][]'

The output Cake needs to create is: <input type="file" name="filename[Model][col][]" /> so that's why there is no column specified.

Check out this blog post from 2012 about multiple file uploads.

Here is a reference from their documentation specifically about the file input in the form helper as well.

treckstar
  • 1,956
  • 5
  • 21
  • 26