php - zf2 how to upload files, more than 1 file


I refer to and follow this, I can upload 1 file successfully by using rename filter in ZF2.

However when I use this way to upload 2 files, it goes wrong. I paste my code as following:

'name' => 'bigpicture',
'attributes' => array(
  'type' => 'file'
  'options' => array(
    'label' => 'Big Pic'
'name' => 'smallpicture',
'attributes' => array(
  'type' => 'file'
'options' => array(
  'label' => 'Small Pic'

<div ><?php echo $this->formRow($form->get('smallpicture')) ?></div>
<div ><?php echo $this->formRow($form->get('bigpicture')) ?></div>

$data = array_merge(

if ($form->isValid()) {
 $picid = $this->getProductTable()->saveProduct($product);
 $pathstr = $this->md5path($picid);
 //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();
  'source' => $data['smallpicture']['tmp_name'],
  'target' => 'public/images/product/'.$pathstr.'/'.$picid.'_small.jpg',
  'overwrite' => true

$fileadapterbig = new \Zend\File\Transfer\Adapter\Http();
  'source' => $data['bigpicture']['tmp_name'],
  'target' => 'public/images/product/'.$pathstr.'/'.$picid.'_big.jpg',
  'overwrite' => true

the above are form,view,action. using this way, only the small picture uploaed successfully. the big picture goes wrong. a warning flashed like the following:

Warning: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 on line 173

Warning: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\file\transfer\adapter\http.php on line 173

Who can tell me how to upload more than 1 file in this way. you know, the rename filter way similar to above. thanks.




I ran into the same problem with a site i did. The solution was to do the renaming in the controller itself by getting all the images and then stepping through them.

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

Hope this helps point you in the right direction.




I encountered the same problem and i managed to make it work using the below code;

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

People are also looking for solutions to the problem: php - MySql Insert Query based on Timestamp (delayed insert)


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.