Search code examples
phpcakephpfile-uploadrequest

CakePHP 2.X upload multiple files at same time


How are you supposed to use arrays of "CakePhpRequest" when uploading multiple files?

I have this result

CakeRequest Object
(
[params] => Array
    (
        [plugin] => 
        [controller] => users
        [action] => upload
        [named] => Array
            (
            )

        [pass] => Array
            (
            )

        [form] => Array
            (
                [files] => Array
                    (
                        [name] => Array
                            (
                                [0] => php.exe
                                [1] => php.gif
                                [2] => php.ini
                                [3] => php.ini-development
                                [4] => php.ini-production
                            )

                        [type] => Array
                            (
                                [0] => application/x-msdownload
                                [1] => image/gif
                                [2] => application/octet-stream
                                [3] => application/octet-stream
                                [4] => application/octet-stream
                            )

                        [tmp_name] => Array
                            (
                                [0] => C:\xampp\tmp\php455E.tmp
                                [1] => C:\xampp\tmp\php456E.tmp
                                [2] => C:\xampp\tmp\php456F.tmp
                                [3] => C:\xampp\tmp\php4570.tmp
                                [4] => C:\xampp\tmp\php4571.tmp
                            )

                        [error] => Array
                            (
                                [0] => 0
                                [1] => 0
                                [2] => 0
                                [3] => 0
                                [4] => 0
                            )

                        [size] => Array
                            (
                                [0] => 73728
                                [1] => 2523
                                [2] => 78907
                                [3] => 72908
                                [4] => 72941
                            )

                    )

            )

    )

[...] other data

do I have to calculate how many files have been uploaded and then iterate all using a number?

Would not it be easier if the output was like this?

CakeRequest Object
(
[params] => Array
    (
        [plugin] => 
        [controller] => users
        [action] => upload
        [named] => Array
            (
            )

        [pass] => Array
            (
            )

        [form] => Array
            (
                [files] => Array
                    (
            [0] => Array(
                [name] => php.exe
                [type] => application/x-msdownload
                [tmp_name] => C:\xampp\tmp\php455E.tmp
                [error] => 0
                [size] => 73728
            )
            [1] => Array(
                [name] => php.gif
                [type] => image/gif
                [tmp_name] => C:\xampp\tmp\php456E.tmp
                [error] => 0
                [size] => 2523
            )
            [...] more data

                    )

            )

    )

[...] other data

This way I only have to use foreach ($files as $file)

Sorry for my bad English


Solution

  • Please specify the field name as.

    <?php
      echo $this->Form->input('files.', array('type' => 'file', 'multiple')); // dont forgot to put . after the name
    ?>
    

    You can get the more details here: https://bakery.cakephp.org/2012/01/31/HTML-5-Multiple-File-Upload-With-Cake.html