I have created a zip file programmatically. Zip is created on destination folder with specific files. But after creating that zip file why I am not getting an option to download (save) or open that zip file in browser ?

Please look in my code.

function cretezipAction()
      $destinationDir = Mage::getBaseDir('var').DS.'productzip'. DS . 'tempZipDir';
     $zip = new ZipArchive();
     $zipFileName = 'productsheets';
     $destinationDir = $destinationDir . DS . $zipFileName . '.zip';

     if(!$zip->open($destinationDir, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE))
        return false;
     $source = Mage::getBaseDir('var').DS.'productfile'.DS.'pdf';
      $someFilePathArray = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);

        foreach($someFilePathArray as $filePath)
            $localname = substr($filePath,strrpos($filePath,'/') + 1); 
            $zip->addFile($filePath, $localname);
        $downloadContent = file_get_contents($destinationDir); 

        $this->_prepareDownloadResponse($zipFileName.'.zip', $downloadContent);

--------------- Update ---------------------

//$downloadContent = file_get_contents($destinationDir); 
$this->_prepareDownloadResponse($zipFileName.'.zip', array( 'value' => $destinationDir, 'type' => 'filename' ));

Even after writing code "prepareDownloadResponse", I am not getting option for save or open zip file.

Thank you in advance.


There is a mistyping in your code, you have got the zip contents into $downloadContent = file_get_contents($destinationDir); but you try to send $content as a response $this->_prepareDownloadResponse($fileName, $content); also I am not sure if $fileName is defined somewhere.


Please modify the last line like this: $this->_prepareDownloadResponse($zipFileName.'.zip', array('value' => $destinationDir, 'type' => 'filename'));

  • Yes I have define $filename. But what to do next ? Can you please help me in this issue ?
    – Aliana
    Jun 8 '16 at 17:22
  • @Aliana try this $this->_prepareDownloadResponse($fileName, $downloadContent);
    – Neklo.com
    Jun 8 '16 at 17:28
  • Same result. :( . zip file saved on destination folder but not give option for download.
    – Aliana
    Jun 8 '16 at 17:33
  • I have found wrong spelling of "prepareDownloadResponse" and I corrected that but now result is zip is created but they ask for download a pdf file which I was added in zip file. instead ask for download zip file.
    – Aliana
    Jun 8 '16 at 17:39
  • @Aliana Please ensure you pass $zipFileName.'.zip' into _prepareDownloadResponse
    – Neklo.com
    Jun 8 '16 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.