我有一個窗體有多個複選框,它允許用戶選擇他們想要的小冊子,然後提示他們下載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