Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple File / Image Uploads CakePHP 3

How do I grab all the data for each file?

I am not sure how to upload multiple files with CakePhp 3 syntax. I have seen posts for plugins, but I would like to know how to do it without having to use a plugin for a simple task.

Here is my form:

<div class="havesAndWants form large-10 medium-9 columns">
    <?= $this->Form->create($havesAndWant, ['type' => 'file']) ?>
    <fieldset>
        <legend><?= __('Add Haves And Want') ?></legend>
        <?php
            echo $this->Form->input('contact_name');
            echo $this->Form->input('contact_email');
            echo $this->Form->input('contact_phone');
            echo $this->Form->input('contact_street_address');
            echo $this->Form->input('contact_city');
            echo $this->Form->input('contact_state');
            echo $this->Form->input('contact_zip');
            echo $this->Form->input('ad_street_address');
            echo $this->Form->input('ad_city');
            echo $this->Form->input('ad_state');
            echo $this->Form->input('ad_zip');
            echo $this->Form->input('ad_additional_info', ['label' => 'Ad Description']);
            echo $this->Form->input('ad_photos', ['type' => 'file', 'multiple' => 'multiple', 'label' => 'Add Some Photos']);
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
</div>

I can't seem to grab all of the data from the multiple files. It is grabbing the first file and spitting out the same info for each file even though they are different files.

Controller:

$photos = $this->request->data['ad_photos'];
foreach ($photos as $photo ) {
                $photo = [
                    'name' => $this->request->data['ad_photos']['name'],
                    'type' => $this->request->data['ad_photos']['type'],
                    'tmp_name' => $this->request->data['ad_photos']['tmp_name'],
                    'error' => $this->request->data['ad_photos']['error'],
                    'size' => $this->request->data['ad_photos']['size']
                ];
                echo "<pre>"; print_r($photo); echo "</pre>";
            }

Output:

Array
(
    [name] => DPC.jpg
    [type] => image/jpeg
    [tmp_name] => C:\wamp\tmp\dpze123.tmp
    [error] => 0
    [size] => 2288982
)
Array
(
    [name] => DPC.jpg
    [type] => image/jpeg
    [tmp_name] => C:\wamp\tmp\dpze123.tmp
    [error] => 0
    [size] => 2288982
)
Array
(
    [name] => DPC.jpg
    [type] => image/jpeg
    [tmp_name] => C:\wamp\tmp\dpze123.tmp
    [error] => 0
    [size] => 2288982
)
Array
(
    [name] => DPC.jpg
    [type] => image/jpeg
    [tmp_name] => C:\wamp\tmp\dpze123.tmp
    [error] => 0
    [size] => 2288982
)
Array
(
    [name] => DPC.jpg
    [type] => image/jpeg
    [tmp_name] => C:\wamp\tmp\dpze123.tmp
    [error] => 0
    [size] => 2288982
)

Notice how it is all the same info?

like image 642
Battousai Avatar asked Apr 18 '26 02:04

Battousai


1 Answers

Thanks to the guys at CakePHPs Support

http://webchat.freenode.net/?channels=cakephp&uio=MT1mYWxzZSY5PXRydWUmMTE9MjQ2b8

I got the answer to the question. I thought I needed to use the $post array that I had in order to set the variables for the file info. In CakePhp Docs it shows an example of this and well, it isn't needed. I took out this block of code:

$photo = [
                    'name' => $this->request->data['ad_photos']['name'],
                    'type' => $this->request->data['ad_photos']['type'],
                    'tmp_name' => $this->request->data['ad_photos']['tmp_name'],
                    'error' => $this->request->data['ad_photos']['error'],
                    'size' => $this->request->data['ad_photos']['size']
                ];

and added a [] to my input name for file uploads like this:

echo $this->Form->input('ad_photos[]', ['type' => 'file', 'multiple' => 'true', 'label' => 'Add Some Photos']);

I was then able to grab all the info for each file.

like image 65
Battousai Avatar answered Apr 20 '26 16:04

Battousai