【问题标题】:zf2 how to upload files, more than 1 filezf2如何上传文件,多于1个文件
【发布时间】:2013-03-24 18:57:20
【问题描述】:

我参考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\vendor\zendframework\zendframework\library\zend\file\transfer\adapter\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 在第 173 行

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢斯蒂芬·格罗布勒!对不起,我仍然不知道如何改进它。我添加 if ($fileadaptersmall->isUploaded()) 和 if ($fileadapterbig->isUploaded()) 在 $fileadatpersmall/big -> receive();仍然,只是上传的小图片。
    【解决方案2】:

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

    $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));
         }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 2012-05-31
      相关资源
      最近更新 更多