嗨,
我有一个带有多个复选框的表单,它允许用户在提示下载 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);
}
}