Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ZF2文件上载集合_Php_File Upload_Zend Framework2 - Fatal编程技术网

Php ZF2文件上载集合

Php ZF2文件上载集合,php,file-upload,zend-framework2,Php,File Upload,Zend Framework2,我无法使我的fileupload集合正常工作。这就是我所做的: 基本上,$request->getFiles()包含正确的信息。因此,上传到PHP本身是可行的。但是,InputFilter一旦在数据上运行,它就会丢失。显然,FileRenameUpload做了一些可疑的事情,我不知道到底是什么。我只知道我的数据丢失了 写权限不是问题所在。我目前正在通过PHP5.5 CLI在windows上的devbox上测试这一点 我在ZF2中验证文件的方式 在composer.json中 在控制器中 看起来

我无法使我的fileupload集合正常工作。这就是我所做的:

基本上,
$request->getFiles()
包含正确的信息。因此,上传到PHP本身是可行的。但是,
InputFilter
一旦在数据上运行,它就会丢失。显然,
FileRenameUpload
做了一些可疑的事情,我不知道到底是什么。我只知道我的数据丢失了

写权限不是问题所在。我目前正在通过PHP5.5 CLI在windows上的devbox上测试这一点

我在ZF2中验证文件的方式

在composer.json中

在控制器中


看起来问题在于元素的集合。在过去,只有字段集被支持在集合中。也许稍后会添加元素支持,但我很确定它有缺陷,因此不能以这种方式为您工作。我重新编写了你的代码,使用了字段集,并对其进行了验证,正确地重命名和移动了上传的文件。问题在于CollectionInputFilter内部,它似乎没有正确更新以支持元素的集合

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

这是我修改过的代码分支:

控制器

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
    ];
}
过滤器(未更改,只是一个小更改)

表格

    $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

您是否尝试过给出绝对路径。检查文件夹权限。正如我提到的,路径信息和文件权限不应该是问题。我已经添加了一个上传文件的答案。这对我很有用。也许对你有帮助我之前也有这个问题。最后,我跳过了FileRenameUpload并实现了我自己的。我正在跟踪一个小错误,在我的例子中,似乎由于某种原因找不到php创建的临时文件。显然,它在FileRenameUpload执行之前就存在了。@cptnk您有没有可能共享您的实现?=)因为这似乎与我遇到的问题完全相同。感谢您的贡献,但是ZF2有内部解决方案,应该可以自己工作。您的控制器中有太多的业务逻辑(尽管这也是我最后的选择…)。就在现场,一个字段集的额外嵌套起了作用。可悲的是,用教义来实现这一点要复杂得多,但对于给定的任务来说,这是正确的答案。恭喜你,非常感谢!
    $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