我有一个窗体有多个复选框,它允许用户选择他们想要的小册子,然后提示他们下载zip文件。 zip档案下载功能正常运行。但我意识到下载会话将被终止而不下载整个文件(比如说8MB),并且下载的zip文件会被破坏。
参考下面的代码,它不能解决问题。任何人都可以建议我需要查看哪些设置,或者我是否错过了任何功能?
if($send) {
// Make sure program execution doesn't time out
// Set maximum script execution time in seconds (0 means no limit)
set_time_limit(0);
$post = $_POST;
$file_folder = "pdf/"; // folder to load files
$zip = new ZipArchive(); // Load zip library
$zip_name = "File-".time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){ // Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time<br/>";
}
foreach($post['brochure'] as $file){
$zip->addFile($file_folder.$file); // Adding files into zip
}
$zip->close();
if(file_exists($zip_name)){
// set headers push to download the zip
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header('Content-type: application/zip');
header("Content-Transfer-Encoding: Binary");
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
header("Content-Length: ".filesize($zip_name));
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
$fp = @fopen($zip_name, "rb");
if ($fp) {
while(!feof($fp)) {
print(fread($fp, 1024*8));
flush(); // this is essential for large downloads
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}
}
嗨文森特,谢谢你的回复。我面临的问题是下载会话总是在几秒钟后终止而不下载整个zip文件。 – Xoozlez 2012-07-18 08:41:43
它对我的解决方案做了同样的事情? 如果下载是在它结束之前被截断的,我不认为它是一个PHP的问题:( – PoulsQ 2012-07-18 20:37:39