gpt4 book ai didi

php - 多个文件/图像上传 CakePHP 3

转载 作者:行者123 更新时间:2023-12-02 17:32:33 25 4
gpt4 key购买 nike

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

我不确定如何使用 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>

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

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

输出:

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
)

注意到所有信息都是一样的吗?

最佳答案

感谢 CakePHPs 支持人员

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

我得到了问题的答案。我想我需要使用我拥有的 $post 数组来设置文件信息的变量。在 CakePhp Docs 中它显示了一个例子,好吧,它不是必需的。我取出了这段代码:

$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']);

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

关于php - 多个文件/图像上传 CakePHP 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31150154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com