ZF2 文件上传:文件为空

KFO*_*KFO 1 php validation file-upload zend-framework2

我在 Zend Framework 2 应用程序中上传图像时遇到问题,我想我的代码中某处存在配置错误。

这是我目前的代码。我像 ZfcUser 表单一样创建了表单和过滤器。

表单扩展提供事件表单(ZfcBase)

$file = new Element\File('image');
$file->setLabelAttributes(array('class' => 'control-label col-sm-4'));
$file->setLabel('image');
$this->add($file);
Run Code Online (Sandbox Code Playgroud)

过滤器扩展提供事件输入过滤器(ZfcBase)

$this->add(
    array(
        'type' => 'Zend\InputFilter\FileInput',
        'name' => 'image',
        'required' => true,
        'validators' => array(
            array(
                'name' => 'File\UploadFile',
            ),
        ),
        'filters' => array(
            array(
                'name' => 'File\RenameUpload',
                'options' => array(
                    'target' => './public/img/uploads',
                    'randomize' => true,
                ),
            ),
        ),
    )
);
Run Code Online (Sandbox Code Playgroud)

在验证过程中,方法 FileInput::isValid() 被调用 - 但值始终为空,我不知道为什么会这样。

HTML-Form设置为multipart/form-data,Server配置也没有问题。我用于测试的文件只有 80KB。

任何人的想法,有什么问题?

KFO*_*KFO 5

最后我找到了解决问题的方法。

在将 Post 信息添加到表单之前,它们必须与文件信息合并:

$files = $this->getRequest()->getFiles()->toArray();
$post = array_merge_recursive(
    $this->getRequest()->getPost()->toArray(),
    $files
);

$form->setData($post);
Run Code Online (Sandbox Code Playgroud)