What i am trying to achieve: Force download a zip file that contains user opted pdf files.
What i did in controller to achieve this:
Generate pdf reports in folder APP.WEBROOT_DIR.DS."package_files" (i used MPDF library) *it generates correct readable pdf. I call here $this->render();
With Zip feature of php, Generate package.zip (which consists pdf files from above specified folder) *it generates correct zip file, when downloaded from server it opens as valid zip file in windows.
Set the controller viewClass to Media and set parameters to force download as zip file, *Again here I call here $this->render(); Issue: When i run i get zip file but when opened with winrar, Zip file obtained reports Unexpected end of archive.
I am not getting any usefull articles to get through this issue...
What i guess is calling two times render is making file corrupt Thanks
My controller code:
/** before this code i generate pdf files and have no issue **/
/** now scan through the directory and add all the pdf files to a zip archive **/
$dir = new Folder("".APP.WEBROOT_DIR.DS."package_files");
$files = $dir->find('.*\.pdf');
$zip = new ZipArchive();
foreach ($files as $file) {
$file_path = $dir->pwd() . DS . $file;
$filename = $dir->pwd() . DS ."package.zip";
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$zip->addFile($file_path,$file);
}
$zip->close();
/** now render the action to download the generated zip file **/
$this->viewClass = 'Media';
$params = array(
'id' => 'package.zip',
'name' => 'packaged_file',
'download' => true,
'extension' => 'zip',
'path' => APP . WEBROOT_DIR.DS.'package_files' . DS
);
$this->set($params);
$this->render();