php - ZF2 文件上传合集

标签 php file-upload zend-framework2

我无法使我的文件上传集合正常工作。这就是我所做的:

https://gist.github.com/manuakasam/1ac71daf7269616f55f0

基本上 $request->getFiles() 包含正确的信息。所以上传到 PHP 本身是有效的。但是一旦 InputFilter 运行数据,所有数据都会丢失。显然 FileRenameUpload 做了一些可疑的事情,我无法弄清楚到底是什么。我只知道我的数据丢失了......

写权限不是问题。我目前正在通过 PHP 5.5 CLI 在 Windows 上的 devbox 上对此进行测试

最佳答案

看起来问题出在元素的集合上。过去,仅支持将 Fieldset 放入集合中。也许稍后会添加元素支持,但我很确定它有问题,因此不会以这种方式为您工作。我重新编写了您的代码以使用 Fieldsets,并验证了它,并正确地重命名和移动了上传的文件。问题出在 CollectionInputFilter 内部,似乎没有正确更新以支持元素集合。

可能很明显:确保在验证之前准备好集合。

这是我修改后的代码分支:https://gist.github.com/netiul/efaf8bd4545d216fd26c

Controller

public function indexAction()
{
    $request = $this->getRequest();

    if($request->isPost())
    {
        $post = array_merge_recursive(
            $request->getPost()->toArray(),
            $request->getFiles()->toArray()
        );

        $this->incidentForm->prepare();

        $this->incidentForm->setData($post);

        if($this->incidentForm->isValid()) {
            \Zend\Debug\Debug::dump($this->incidentForm->getData());
            die();
        }
    }

    return [
        'form' => $this->incidentForm
    ];
}

TheFilter(没有改变,一个小的改变)

    $singleFileFilter = new InputFilter();
    $singleFileFilter->add(
        [
            'name'     => 'attachment',
            'required' => false,
            'filters'  => [
                [
                    'name'    => 'filerenameupload',
                    'options' => [
                        'target'          => 'data/incident_attachments/', /* small change here, removed the slash in front of data to make the path relative */
                        'randomize'       => true,
                        'use_upload_name' => false,
                        'overwrite'       => false
                    ]
                ]
            ]
        ]
    );

    $attachedFilesFilter = new CollectionInputFilter();
    $attachedFilesFilter->setInputFilter($singleFileFilter);

    $this->add($attachedFilesFilter, 'attachedFiles');

形式

    $this->setAttribute('enctype', "multipart/form-data");

    $fileElement = new Form\Element\File('attachment');
    $fileElement->setOptions(
        [
            'label'            => 'Add Attachment',
            'label_attributes' => [
                'class' => 'control-label col-sm-3'
            ]
        ]
    );
    $fileElement->setAttributes([
            'class' => 'form-control'
        ]);

    $collectionTarget = new Form\Fieldset('uploadItem');
    $collectionTarget->add($fileElement);

    $this->add(
        [
            'name'    => 'attachedFiles',
            'type'    => 'collection',
            'options' => [
                'count'          => 3,
                'target_element' => $collectionTarget
            ]
        ]
    );

验证后的输出

array (size=1)
  'attachedFiles' => 
    array (size=3)
      0 => 
        array (size=1)
          'attachment' => 
            array (size=5)
              'name' => string '1326459.png' (length=11)
              'type' => string 'image/png' (length=9)
              'tmp_name' => string 'data/incident_attachments_538c30fe0fb2f' (length=39)
              'error' => int 0
              'size' => int 791802
      1 => 
        array (size=1)
          'attachment' => 
            array (size=5)
              'name' => string '1510364_10152488321303345_257207784_n.jpg' (length=41)
              'type' => string 'image/jpeg' (length=10)
              'tmp_name' => string 'data/incident_attachments_538c30fec55c4' (length=39)
              'error' => int 0
              'size' => int 53272
      2 => 
        array (size=1)
          'attachment' => 
            array (size=5)
              'name' => string 'IMG_20140430_095013.jpg' (length=23)
              'type' => string 'image/jpeg' (length=10)
              'tmp_name' => string 'data/incident_attachments_538c30ff4489d' (length=39)
              'error' => int 0
              'size' => int 1039118

关于php - ZF2 文件上传合集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23953525/

相关文章:

php - 使用php从mysql数据库下载的文件被更改

mysql - Zend Framework 2 无法从联接表中获取字段

php - ZF2 - 创建自定义表单 View 助手

php - sudo 与 libssh2

php - 有没有一种方法可以检查多个与关系表中最小值的匹配?

php - 如何从文本文件中回显随机行

java - 将文件下载到客户端系统中的特定位置

c# - 在 Asp.net 中使用 HTML5 的拖放上传文件

php - Zf2 float 验证无效但验证器消息为空

php - 在 CakePHP 的 Router 文件中获取 Controller 名称和 SQL 查询?