多个文件/图像上传CakePHP 3

时间:2015-06-30 23:22:15

标签: php cakephp cakephp-3.0

如何获取每个文件的所有数据?

我不确定如何使用CakePhp 3语法上传多个文件。我已经看过插件的帖子,但我想知道怎么做而不必使用插件来完成一个简单的任务。

这是我的表格:

<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>

我似乎无法从多个文件中获取所有数据。它抓取第一个文件并为每个文件吐出相同的信息,即使它们是不同的文件。

控制器:

$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>";
            }

输出:

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
)

注意它是如何相同的信息?

1 个答案:

答案 0 :(得分:5)

感谢CakePHPs支持人员

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

我得到了问题的答案。我以为我需要使用我的$ post数组来设置文件信息的变量。在CakePhp文档中,它显示了一个这样的例子,并且不需要它。我拿出了这段代码:

$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 $this->Form->input('ad_photos[]', ['type' => 'file', 'multiple' => 'true', 'label' => 'Add Some Photos']);

然后我能够获取每个文件的所有信息。