CakePHP 3.x多文件上传无法正常工作

时间:2016-06-03 19:10:07

标签: php cakephp-3.x cakephp-3.1

使用该行时:

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

表单甚至不会提交。就像页面重新加载一样。此代码用于工作。另外,如果我使用ad_photos。而不是ad_photos []我收到此错误消息:

Cannot get an empty property

如果我只使用没有点或括号的ad_photos,则此请求中只显示一个文件 - >数据。我已经尝试调试和var_dump数据,如下所示,但它甚至没有使用括号进入代码的那部分,并且仅使用点抛出错误。

控制器方法:

public function add()
{
    $listing = $this->HavesAndWants->newEntity();
    $error = '';
    if ($this->request->is('post')) {
        debug($this->request->data()); exit;
        echo "<pre>"; var_dump($this->request->data()); echo "</pre>"; exit;
        $listing = $this->HavesAndWants->patchEntity($listing, $this->request->data);
        $listing->user_id = $this->Auth->user('id');
        $listing->ad_photos = '';
        if ( $error ) {
            $this->Flash->error(__('The listing could not be saved. Please, try again.'));
        } else {
            if ($this->HavesAndWants->save($listing)) {
                $this->Flash->success(__('The listing has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The listing could not be saved. Please, try again.'));
            }
        }
    }
    $this->set(compact('error', 'listing'));
    $this->set('_serialize', ['listing']);
}

查看表单:

<?= $this->Form->create($listing, ['type' => 'file']) ?>
        <h1><?= __('Add Listing') ?></h1>
        <fieldset>
            <legend><?= __('Contact Info') ?></legend>
            <?= $this->Form->input('contact_name'); ?>
            <?= $this->Form->input('contact_email'); ?>
            <?= $this->Form->input('contact_phone'); ?>
            <?= $this->Form->input('contact_street_address'); ?>
            <?= $this->Form->input('contact_city'); ?>
            <?= $this->Form->input('contact_state'); ?>
            <?= $this->Form->input('contact_zip'); ?>
        </fieldset>
        <fieldset>
            <legend><?= __('Listing Info') ?></legend>
            <?= $this->Form->input('ad_title'); ?>
            <?= $this->Form->input('ad_street_address'); ?>
            <?= $this->Form->input('ad_city'); ?>
            <?= $this->Form->input('ad_state'); ?>
            <?= $this->Form->input('ad_zip'); ?>
            <?= $this->Form->input('ad_additional_info', ['label' => 'Ad Description']); ?>
            <?= $this->Form->input('ad_photos[]', ['type' => 'file', 'multiple' => true, 'label' => 'Add Some Photos']); ?>
        </fieldset>
        <?= $this->Form->button(__('Submit')) ?>
        <?= $this->Form->end() ?>

1 个答案:

答案 0 :(得分:0)

我觉得有点愚蠢,但是安全组件让我不知所措。我所要做的就是解锁ad_photos字段,我可以上传多个文件。虽然,我不确定为什么我一开始就被黑洞包围了。如果有人有任何想法,请在下面发布。 :)