How do you zip 3 small text files and force download with Zend Framework
Is there a Zend Framework method to save content from 3 files (be they dynamically generated or actually exist) and force download as a file开发者_开发知识库?
Similar to this question (which didn't work for me when running from inside a controller so far, despite trying a few different ways):
PHP Zip 3 small text files and force download
You can use the PHP ZIP library (you need to have that preinstalled) like that:
$zip = new ZipArchive();
if($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true){
throw new Exception('Could not create zip file ' . $filename);
die('zip fail');
}else{
$zip->addFile($file1Uri, 'file1.txt');
$zip->addFile($file2Uri, 'file2.txt');
}
$zip->close();
if(file_exists($filename)){
return true;
}else{
throw new Exception('Could not create zip file ' . $filename);
}
Deliver the ZIP file:
protected function _deliver($file, $name, $extension, $size, $mime){
header('Pragma: private');
header("Expires: -1");
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header("Cache-Control: no-cache");
header("Content-Transfer-Encoding: binary");
header("Content-Type: " . $mime);
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $name . '.' . $extension . '"');
header("Content-Length: " . $size);
set_time_limit(0);
if(!readfile($file)){
return false;
}
}
The answer is the upvoted one on your other question. Do it from controller, then call exit
after you output the zip data so don't you render the view.
精彩评论