问题描述
我无法让我的文件上传集合工作。这是我所做的:
基本上 $ request-> getFiles()
包含正确的信息。所以上传到PHP本身的作品。但是一旦 InputFilter
在数据上运行,它就会全部丢失。 Apprently FileRenameUpload
有些诡异,我不知道究竟是什么。我所知道的是我的数据丢失了...
写入权限不是问题。我正在通过PHP 5.5 CLI在我的开发板上测试这个目录。 >元素即可。过去只有Fieldsets被支持在一个集合中。也许元素支持稍后添加,但我很确定它是越野车,因此不以这种方式为你工作。我重写了你的代码,使用Fieldsets,我得到验证,上传的文件正确地重命名和移动。问题出在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
];
$ b TheFilter(没有改变,改变很小)
$ singleFileFilter = new InputFilter();
$ singleFileFilter-> add(
[
'name'=>'attachment',
'required'=> false,
'filters'= > [
[
'name'=>'filerenameupload',
'options'=> [
'target'=>'data / incident_attachments /', / *小的改变,删除数据前面的斜线,使路径相对* /
'randomize'=> true,
'use_upload_name'=> false,
'覆盖'=> false
]
]
]
]
);
$ attachedFilesFilter = new CollectionInputFilter();
$ attachedFilesFilter-> setInputFilter($ singleFileFilter);
$ this-> add($ attachedFilesFilter,'attachedFiles');
TheForm
$ 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);
$ b $ 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'=>字符串'1326459.png'(length = 11)
'type'=>字符串'image / png'(长度= 9)
'tmp_name'=>字符串'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'=>字符串'image / jpeg'(长度= 10)
'tmp_name'=>字符串'data / incident_attachments_538c30fec55c4'(length = 39)
'error'=> int 0
'size'=> int 53272
2 =>
array(size = 1)
'attachment'=>
array(size = 5)
'name'=> 'IMG_20140430_095013.jpg'(length = 23)
'type'=>字符串'image / jpeg'(长度= 10)
'tmp_name'=>字符串'data / incident_attachments_538c30ff4489d'(length = 39)
'error'=> int 0
'size'=> int 1039118
I am unable to get my fileupload collection to work. This is what I've done:
Basically the $request->getFiles()
contains the proper information. So the upload to PHP itself works. But once the InputFilter
runs over the data, it all gets lost. Apprently FileRenameUpload
does something fishy and I can't figure out what exactly. All I know is my data gets lost...
Write permissions are not the problem. I'm testing this currently on my devbox on windows via PHP 5.5 CLI
解决方案 Looks like the problem is the Collection of Elements. In the past only Fieldsets were supported to be in a Collection. Maybe Element support is added later, but I'm pretty sure it's buggy and therefore not working for you in this way. I reworked your code to be using Fieldsets and I got it validated and the uploaded files properly renamed and moved. The problem was inside the CollectionInputFilter, that one doesn't seem to be correctly updated to support collection of Elements.
Probably obvious: make sure to prepare the collection before validation.
Here's my modified fork of your code: 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 (not changed, well a small change)
$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');
TheForm
$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
]
]
);
Output after validation
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
这篇关于ZF2 FileUpload集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!