ylliX - Online Advertising Network Zend_File_Transfer - uploading same file multiple times - Yet Another Programmer's Blog

Zend_File_Transfer – uploading same file multiple times

I faced with such problem: had one upload field for one file in my Zend_Form, and few checkboxes for choosing file destination. After form is submitted, this one file should be uploaded to each locations checked in form. So first idea was to loop through checked checkboxes, in each iteration set:

$upload->addFilter('Rename', array('target' => $path . '/' . $filename, 'overwrite' => true));

then do:

$upload->receive($file);

and it should be done, but its not. Problem is in

$files = $upload->getFileInfo();

which is called BEFORE loop, and which can’t be modified INSIDE loop. After each receive() file is marked as already received and is not processed again. My solution was not state-of-art but works: files are received BEFORE checkbox loop, added to array, then loop is done and each file is copied where it should be. It looks this way:

$files = $upload->getFileInfo();
$filesUploaded = array();
foreach ($files as $file => $info) {
   if ($upload->isUploaded($file) && $upload->isValid($file)) {
       if ($upload->receive($file)) {
            $name = $upload->getFileName($file);
            $filesUploaded[] = $name;
       }else{
            // some error handling here
       }
   }else{
      // some error handling here 
   }
}

foreach ($filesUploaded as $file) {
   foreach ($post['myCheckbox'] as $id) {
       copy($src, $dest);
   }
}

 

2 thoughts on “Zend_File_Transfer – uploading same file multiple times

Leave a Reply