2013-04-03 96 views
3

我參考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個文件。你知道,重命名過濾器的方式類似於上面。謝謝。

回答

2

我遇到了同樣的問題,我的網站。解決方案是通過獲取所有圖像然後逐步完成控制器本身的重命名。

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

希望這可以幫助您指出正確的方向。

+0

謝謝Stephan Grobler!對不起,我還不知道如何改進它。我在$ fileadatpersmall/big - > receive();之外添加if($ fileadaptersmall-> isUploaded())和if($ fileadapterbig-> isUploaded())。仍然只是上傳的小圖片。 –

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)); 
    } 
} 
相關問題