zf2如何上传文件,超过1个文件

时间:2013-04-03 07:52:56

标签: php file-upload file-io zend-framework2

我引用http://samsonasik.wordpress.com/2012/08/31/zend-framework-2-creating-upload-form-file-validation/并按照此说法,我可以使用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):无法打开流:无效的参数   E:\ MyProject的\供应商\ zendframework \ zendframework \库\ Zend的\文件\传输\适配器\ http.php   在第173行

     

警告:move_uploaded_file():无法移动'C:\ WINDOWS \ TMP \ php76.tmp'   到'C:\ WINDOWS \ TEMP \ big.jpg'中   E:\ MyProject的\供应商\ zendframework \ zendframework \库\ Zend的\文件\传输\适配器\ http.php   在第173行

谁能告诉我如何以这种方式上传多个文件。你知道,重命名过滤方式与上面类似。感谢。

2 个答案:

答案 0 :(得分:2)

我遇到了与我所做的网站相同的问题。解决方案是通过获取所有图像然后单步调试控制器本身来重命名。

if ($file->isUploaded()) {
            $pInfo = pathinfo($file->getFileName());
            $time = uniqid();
            $newName = $pName . '-' . $type . '-' . $time . '.' . $pInfo['extension'];
            $file->addFilter('Rename', array('target' => $newName));
            $file->receive();
        }

希望这有助于指明你正确的方向。

答案 1 :(得分:0)

我遇到了同样的问题,我设法使用以下代码使其工作;

$folder = 'YOUR DIR';        
$adapter = new \Zend\File\Transfer\Adapter\Http();
$adapter->setDestination($folder);
foreach ($adapter->getFileInfo() as $info) {
     $originalFileName = $info['name'];
     if ($adapter->receive($originalFileName)) {          
         $newFilePath = $folder . '/' . $newFileName;
         $adapter->addFilter('Rename', array('target' => $newFilePath,
             'overwrite' => true));
     }
 }