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

816

I refer to http://samsonasik.wordpress.com/2012/08/31/zend-framework-2-creating-upload-form-file-validation/ 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:

$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 ><?php echo $this->formRow($form->get('smallpicture')) ?></div>
<div ><?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();
}  

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.

600

Answer

Solution:

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

Hope this helps point you in the right direction.

561

Answer

Solution:

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

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

Source

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.