我参考http://samsonasik.wordpress.com/2012/08/31/zend-framework-2-creating-upload-form-file-validation/并按照这个,我可以通过使用重命名过滤器成功上传1个文件ZF2。
但是,当我使用这种方式上传 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):failed to open stream:Invalid argument in E:\myproject\vendor\zendframework\zendframework\library\zend\file\transfer\adapter\http.php 在线173
警告:move_uploaded_file():Unable to move 'C:\WINDOWS\TMP\php76.tmp' to 'C:\WINDOWS\TEMP\big.jpg' in E:\myproject\vendor\zendframework\zendframework\library\zend\第 173 行的文件\传输\适配器\http.php
谁能告诉我如何以这种方式上传多个文件。你知道的,重命名过滤方式和上面类似。谢谢。