# Uploading multiple files. Error Invalid data type, must be an array or \\ArrayAccess instance

**URL:** <https://discourse.cakephp.org/t/uploading-multiple-files-error-invalid-data-type-must-be-an-array-or-arrayaccess-instance/5117>\
**Category:** Need Help\
**Created:** [October 18, 2018, 8:36am UTC](https://discourse.cakephp.org/t/uploading-multiple-files-error-invalid-data-type-must-be-an-array-or-arrayaccess-instance/5117 "2018-10-18T08:36:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mr.Velazquez](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@Mr.Velazquez](https://discourse.cakephp.org/u/Mr.Velazquez)\
**Post date:** [October 18, 2018, 8:36am UTC](https://discourse.cakephp.org/t/uploading-multiple-files-error-invalid-data-type-must-be-an-array-or-arrayaccess-instance/5117/1 "2018-10-18T08:36:45Z")

</div>

Hello everyone i just started out using cakephp and php in general. First thank you Jose Gonzalez for the Plugin it really did help me a lot, while figuring out how to upload pictures.

Multiple file upload works for me when only choosing two pictures, once i extend that to 3 or 4 i get this error message: Invalid data type, must be an array or \ArrayAccess instance.

I already got the suggestion to change the:  
post\_max\_size=20M  
upload\_max\_filesize=20M  
And even though the total upload size is 500kb i still get an error message.

Here is the part of the Table I am working on. Thanks in advance for your help.

public function initialize(array $config)  
{  
$this-\>setPrimaryKey(‘id’);  
$this-\>belongsToMany(‘Tags’);  
$this-\>addBehavior(‘Timestamp’);

```auto
    $this->addBehavior('Josegonzalez/Upload.Upload', [
        //works with two
        'photolg',
        //stops working with 3rd and 4th
        //'photomd',
        //'photosm',
        'photo' => [
            'fields' => [
                'dir' => 'photo_dir',
            ],
            'nameCallback' => function ($table, $entity, $data, $field, $settings) {
                return strtolower($data['name']);
            },
            'transformer' => function ($table, $entity, $data, $field, $settings) {
                $extension = pathinfo($data['name'], PATHINFO_EXTENSION);

                // Store the thumbnail in a temporary file
                $tmp = tempnam(sys_get_temp_dir(), 'upload') . '.' . $extension;

                // Use the Imagine library to DO THE THING
                $size = new \Imagine\Image\Box(100, 100);
                $mode = \Imagine\Image\ImageInterface::THUMBNAIL_INSET;
                $imagine = new \Imagine\Gd\Imagine();

                // Save that modified file to our temp file
                $imagine->open($data['tmp_name'])
                    ->thumbnail($size, $mode)
                    ->save($tmp);

                // Now return the original *and* the thumbnail
                return [
                    $data['tmp_name'] => $data['name'],
                    $tmp => 'thumbnail-' . $data['name'],
                ];
            },
            'deleteCallback' => function ($path, $entity, $field, $settings) {
                // When deleting the entity, both the original and the thumbnail will be removed
                // when keepFilesOnDelete is set to false
                return [
                    $path . $entity->{$field},
                    $path . 'thumbnail-' . $entity->{$field}
                ];
            },
            'keepFilesOnDelete' => false
        ]
    ]);
}

```

---

<div class="post-metadata">

**Author:** ![Mr.Velazquez](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@Mr.Velazquez](https://discourse.cakephp.org/u/Mr.Velazquez)\
**Post date:** [October 18, 2018, 9:37am UTC](https://discourse.cakephp.org/t/uploading-multiple-files-error-invalid-data-type-must-be-an-array-or-arrayaccess-instance/5117/2 "2018-10-18T09:37:25Z")

</div>

I got help over at GitHub, and the Problem is now solved. The working table looks like this now (NOT PRETTY and a lot of repeating if you got a better solution let me know please!):

public function initialize(array $config)  
{  
 $this-\>setPrimaryKey(‘id’);  
 $this-\>belongsToMany(‘Tags’);  
 $this-\>addBehavior(‘Timestamp’);

$this-\>addBehavior(‘Josegonzalez/Upload.Upload’, [

‘photolg’=\> [  
 ‘fields’ =\> [  
 ‘dir’ =\> ‘photolg\_dir’,  
 ],  
 ‘nameCallback’ =\> function ($table, $entity, $data, $field, $settings) {  
 return strtolower($data[‘name’]);  
 },  
 ‘transformer’ =\> function ($table, $entity, $data, $field, $settings) {  
 $extension = pathinfo($data[‘name’], PATHINFO\_EXTENSION);

// Store the thumbnail in a temporary file  
 $tmp = tempnam(sys\_get\_temp\_dir(), ‘upload’) . ‘.’ . $extension;

// Use the Imagine library to DO THE THING  
 $size = new \Imagine\Image\Box(100, 100);  
 $mode = \Imagine\Image\ImageInterface::THUMBNAIL\_INSET;  
 $imagine = new \Imagine\Gd\Imagine();

// Save that modified file to our temp file  
 $imagine-\>open($data[‘tmp\_name’])  
 -\>thumbnail($size, $mode)  
 -\>save($tmp);

// Now return the original _and_ the thumbnail  
 return [  
 $data[‘tmp\_name’] =\> $data[‘name’],  
 $tmp =\> ‘thumbnail-’ . $data[‘name’],  
 ];  
 },  
 ‘deleteCallback’ =\> function ($path, $entity, $field, $settings) {  
 // When deleting the entity, both the original and the thumbnail will be removed  
 // when keepFilesOnDelete is set to false  
 return [  
 $path . $entity-\>{$field},  
 $path . ‘thumbnail-’ . $entity-\>{$field}  
 ];  
 },  
 ‘keepFilesOnDelete’ =\> false  
 ],

‘photomd’=\> [  
 ‘fields’ =\> [  
 ‘dir’ =\> ‘photomd\_dir’,  
 ],  
 ‘nameCallback’ =\> function ($table, $entity, $data, $field, $settings) {  
 return strtolower($data[‘name’]);  
 },  
 ‘transformer’ =\> function ($table, $entity, $data, $field, $settings) {  
 $extension = pathinfo($data[‘name’], PATHINFO\_EXTENSION);

// Store the thumbnail in a temporary file  
 $tmp = tempnam(sys\_get\_temp\_dir(), ‘upload’) . ‘.’ . $extension;

// Use the Imagine library to DO THE THING  
 $size = new \Imagine\Image\Box(100, 100);  
 $mode = \Imagine\Image\ImageInterface::THUMBNAIL\_INSET;  
 $imagine = new \Imagine\Gd\Imagine();

// Save that modified file to our temp file  
 $imagine-\>open($data[‘tmp\_name’])  
 -\>thumbnail($size, $mode)  
 -\>save($tmp);

// Now return the original _and_ the thumbnail  
 return [  
 $data[‘tmp\_name’] =\> $data[‘name’],  
 $tmp =\> ‘thumbnail-’ . $data[‘name’],  
 ];  
 },  
 ‘deleteCallback’ =\> function ($path, $entity, $field, $settings) {  
 // When deleting the entity, both the original and the thumbnail will be removed  
 // when keepFilesOnDelete is set to false  
 return [  
 $path . $entity-\>{$field},  
 $path . ‘thumbnail-’ . $entity-\>{$field}  
 ];  
 },  
 ‘keepFilesOnDelete’ =\> false  
 ],

‘photosm’=\> [  
 ‘fields’ =\> [  
 ‘dir’ =\> ‘photosm\_dir’,  
 ],  
 ‘nameCallback’ =\> function ($table, $entity, $data, $field, $settings) {  
 return strtolower($data[‘name’]);  
 },  
 ‘transformer’ =\> function ($table, $entity, $data, $field, $settings) {  
 $extension = pathinfo($data[‘name’], PATHINFO\_EXTENSION);

// Store the thumbnail in a temporary file  
 $tmp = tempnam(sys\_get\_temp\_dir(), ‘upload’) . ‘.’ . $extension;

// Use the Imagine library to DO THE THING  
 $size = new \Imagine\Image\Box(100, 100);  
 $mode = \Imagine\Image\ImageInterface::THUMBNAIL\_INSET;  
 $imagine = new \Imagine\Gd\Imagine();

// Save that modified file to our temp file  
 $imagine-\>open($data[‘tmp\_name’])  
 -\>thumbnail($size, $mode)  
 -\>save($tmp);

// Now return the original _and_ the thumbnail  
 return [  
 $data[‘tmp\_name’] =\> $data[‘name’],  
 $tmp =\> ‘thumbnail-’ . $data[‘name’],  
 ];  
 },  
 ‘deleteCallback’ =\> function ($path, $entity, $field, $settings) {  
 // When deleting the entity, both the original and the thumbnail will be removed  
 // when keepFilesOnDelete is set to false  
 return [  
 $path . $entity-\>{$field},  
 $path . ‘thumbnail-’ . $entity-\>{$field}  
 ];  
 },  
 ‘keepFilesOnDelete’ =\> false  
 ],

‘photo’ =\> [  
 ‘fields’ =\> [  
 ‘dir’ =\> ‘photo\_dir’,  
 ],  
 ‘nameCallback’ =\> function ($table, $entity, $data, $field, $settings) {  
 return strtolower($data[‘name’]);  
 },  
 ‘transformer’ =\> function ($table, $entity, $data, $field, $settings) {  
 $extension = pathinfo($data[‘name’], PATHINFO\_EXTENSION);

// Store the thumbnail in a temporary file  
 $tmp = tempnam(sys\_get\_temp\_dir(), ‘upload’) . ‘.’ . $extension;

// Use the Imagine library to DO THE THING  
 $size = new \Imagine\Image\Box(100, 100);  
 $mode = \Imagine\Image\ImageInterface::THUMBNAIL\_INSET;  
 $imagine = new \Imagine\Gd\Imagine();

// Save that modified file to our temp file  
 $imagine-\>open($data[‘tmp\_name’])  
 -\>thumbnail($size, $mode)  
 -\>save($tmp);

// Now return the original _and_ the thumbnail  
 return [  
 $data[‘tmp\_name’] =\> $data[‘name’],  
 $tmp =\> ‘thumbnail-’ . $data[‘name’],  
 ];  
 },  
 ‘deleteCallback’ =\> function ($path, $entity, $field, $settings) {  
 // When deleting the entity, both the original and the thumbnail will be removed  
 // when keepFilesOnDelete is set to false  
 return [  
 $path . $entity-\>{$field},  
 $path . ‘thumbnail-’ . $entity-\>{$field}  
 ];  
 },  
 ‘keepFilesOnDelete’ =\> false  
 ]  
 ]);  
}
