3

我参考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

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

4

2 回答 2

2

我在一个网站上遇到了同样的问题。解决方案是通过获取所有图像然后单步执行它们来在控制器本身中进行重命名。

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

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

于 2013-04-04T07:37:21.000 回答
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));
     }
 }
于 2013-06-13T03:46:03.387 回答