我无法让我的fileupload集合正常工作。这就是我所做的:
基本上$request->getFiles()
包含正确的信息。所以上传到PHP本身就可以了。但是一旦InputFilter
运行数据,它就会丢失。显然FileRenameUpload
做了一些蠢事,我无法弄清楚到底是什么。我所知道的是我的数据丢失了......
写权限不是问题。我正在通过PHP 5.5 CLI在Windows上的devbox上测试这个
答案 0 :(得分:6)
看起来问题是元素的集合。过去,只有Fieldsets支持在Collection中。也许稍后会添加Element支持,但我很确定它是错误的,因此不会以这种方式为您工作。我重新编写了代码以使用Fieldsets,我对其进行了验证,并且上传的文件已正确重命名并移动。问题出在CollectionInputFilter中,似乎没有正确更新以支持元素集合。
可能很明显:确保在验证前准备好这个集合。
这是我修改过的代码分支:https://gist.github.com/netiul/efaf8bd4545d216fd26c
<强>控制器强>
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');
<强> 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
]
]
);
验证后输出
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
答案 1 :(得分:0)
我在ZF2中验证文件的方式
在composer.json
中
"imagine/Imagine": "0.3.*",
控制器中的
use Zend\Validatior\File\Size;
use Imagine\Gd\Imagine;
use Imagine\Image\Box;
use Imagine\Image\Point;
public function imageAction()
{
$id = ( int ) $this->params ()->fromRoute ( 'id', 0 );
if (! $id) {
return $this->redirect ()->toRoute ( 'newscategory' );
}
$form = new YourForm();
$form->get ( 'link_id' )->setAttribute ( 'value', $id );
$request = $this->getRequest();
if ($request->isPost()) {
// Make certain to merge the files info!
$image = new Entityimage();
$form->setInputFilter($image->getInputFilter());
$post = array_merge_recursive(
$request->getPost()->toArray(),
$request->getFiles()->toArray()
);
$form->setData($post);
if ($form->isValid()) {
$size = new \Zend\Validator\File\ImageSize(array(
'minWidth' => 30, 'minHeight' => 30,
'maxWidth' => 1024, 'maxHeight' => 920,
)); //minimum bytes filesize
$isImage = new \Zend\Validator\File\IsImage();
$mimeType = new \Zend\Validator\File\MimeType(array('image/gif', 'image/jpg','image/jpeg','image/png','enableHeaderCheck' => true));
$adapter = new \Zend\File\Transfer\Adapter\Http();
$adapter->setValidators(array($size), $post['fileupload']['name']);
$adapter->setValidators(array($isImage), $post['fileupload']['name']);
$adapter->setValidators(array($mimeType), $post['fileupload']['name']);
if (!$adapter->isValid()){
$dataError = $adapter->getMessages();
$error = array();
foreach($dataError as $key=>$row)
{
$error[] = $row;
}
$form->setMessages(array('fileupload'=>$error ));
$messages = $form->getMessages();
return $this->redirect ()->toRoute ( 'your route' );
// print_r($messages);die('file errors');
} else {
$adapter->setDestination('images/yourdir');
if ($adapter->receive($post['fileupload']['name'])) {
$image->exchangeArray($form->getData());
switch(strtolower($_FILES['fileupload']['type']))
{
case 'image/jpeg':
$filename = imagecreatefromjpeg('images/yourdir/'.$post['fileupload']['name']);
break;
case 'image/png':
$filename = imagecreatefrompng('images/yourdir/'.$post['fileupload']['name']);
break;
case 'image/gif':
$filename = imagecreatefromgif('images/yourdir/'.$post['fileupload']['name']);
break;
default:
exit('Unsupported type: '.$_FILES['fileupload']['type']);
}
ob_start();
imagejpeg($filename);
// large image
$large = base64_encode(ob_get_contents()); // returns output
$mainimgWidth = imagesx($filename);
$mainimgHeight = imagesy($filename);
$thumbWidth = 48; //intval($mainimgWidth / 4);
$thumbHeight = floor( $mainimgHeight * ( $thumbWidth / $mainimgWidth ) ); //intval($mainimgHeight / 4);
$new = imagecreatetruecolor($thumbWidth, $thumbHeight);
$backgroundColor = imagecolorallocate($new, 255, 255, 255);
imagefill($new, 0, 0, $backgroundColor);
imagecopyresampled($new, $filename, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $mainimgWidth, $mainimgHeight);
/** Catch the imagedata */
ob_start();
imagejpeg($new);
$data = ob_get_clean();
// Destroy resources
imagedestroy($filename);
imagedestroy($new);
// Set new content-type and status code
$thumb = base64_encode($data);
// imagine library was intsalled by the composer in the test server by amarjit
ob_end_clean();
$imagine = new Imagine();
//rename files
$filedata = array(
'id' => $post ['id'],
'news_link_id' => $post ['news_link_id'],
'alt' => $post ['alt'],
'detail' => $post ['detail'],
'active' => $post ['active'],
'thumb' => $thumb,
'large' => $large,
'created' => date('Y-m-d H:i:s'),
'createdby' => 1,
);
$id = $this->getImageTable ()->save ( $filedata );
$imagine->open('images/yourdir/'.$post['fileupload']['name'])
->save('images/yourdir/filename-'.$id.'.jpg');
/**
* delete the origional uploaded file;
*/
unlink('images/yourdir/'.$post['fileupload']['name']);
return $this->redirect ()->toRoute ( 'newscategory' );
}
}
}else{
$messages = $form->getMessages();
}
}
return array (
'id' => $id,
'form' => $form,
);
}