我參考http://samsonasik.wordpress.com/2012/08/31/zend-framework-2-creating-upload-form-file-validation/並按照這個,我可以在ZF2中使用重命名過濾器成功上傳1個文件。zf2如何上傳文件,超過1個文件
但是當我用這種方式上傳2個文件時,它出錯了。我粘貼我的代碼如下:
$this->add(array(
'name' => 'bigpicture',
'attributes' => array(
'type' => 'file'
),
'options' => array(
'label' => 'Big Pic'
)
));
$this->add(array(
'name' => 'smallpicture',
'attributes' => array(
'type' => 'file'
),
'options' => array(
'label' => 'Small Pic'
)
));
<div class="row"><?php echo $this->formRow($form->get('smallpicture')) ?></div>
<div class="row"><?php echo $this->formRow($form->get('bigpicture')) ?></div>
$data = array_merge(
$request->getPost()->toArray(),
$request->getFiles()->toArray()
);
$form->setData($data);
if ($form->isValid()) {
$product->exchangeArray($form->getData());
$picid = $this->getProductTable()->saveProduct($product);
$pathstr = $this->md5path($picid);
$this->folder('public/images/product/'.$pathstr);
//move_uploaded_file($data['smallpicture']['tmp_name'], 'public/images/product/'.$pathstr.'/'.$picid.'_small.jpg');
//move_uploaded_file($data['bigpicture']['tmp_name'], 'public/images/product/'.$pathstr.'/'.$picid.'_big.jpg');
$fileadaptersmall = new \Zend\File\Transfer\Adapter\Http();
$fileadaptersmall->addFilter('File\Rename',array(
'source' => $data['smallpicture']['tmp_name'],
'target' => 'public/images/product/'.$pathstr.'/'.$picid.'_small.jpg',
'overwrite' => true
));
$fileadaptersmall->receive();
$fileadapterbig = new \Zend\File\Transfer\Adapter\Http();
$fileadapterbig->addFilter('File\Rename',array(
'source' => $data['bigpicture']['tmp_name'],
'target' => 'public/images/product/'.$pathstr.'/'.$picid.'_big.jpg',
'overwrite' => true
));
$fileadapterbig->receive();
}
以上是窗體,視圖,動作。 採用這種方式,只有成功上傳的小圖片。大的情況出錯了。 警告閃蒸類似如下:
警告:move_uploaded_file(C:\ Windows \ TMP \ small.jpg):未能打開流:在 ë參數無效:\ myproject的\廠商\ zendframework \ zendframework \庫\ Zend的\文件\傳輸\適配器\ http.php 上線173
警告:move_uploaded_file()以:無法移動 'C:\ Windows \ TMP \ php76.tmp' 到「C:\ Windows \ TEMP \ big.jpg'在 E:\ myproject \ vendor \ zendframework \ zendframework \ library \ zend \ file \ transfer \ adapter \ http.php on line 173
誰能告訴我如何以這種方式上傳超過1個文件。你知道,重命名過濾器的方式類似於上面。謝謝。
謝謝Stephan Grobler!對不起,我還不知道如何改進它。我在$ fileadatpersmall/big - > receive();之外添加if($ fileadaptersmall-> isUploaded())和if($ fileadapterbig-> isUploaded())。仍然只是上傳的小圖片。 –