【发布时间】:2013-11-29 05:26:32
【问题描述】:
我制作了一个脚本,可以压缩服务器上的某些文件并下载,但问题是在下载 zip 时我无法在网站上的任何位置导航。它会卡住,直到下载未完成或取消。我正在使用以下代码制作 zip 并下载它
<?php
$files = $_SESSION['cart']['all'];
function createZip($files, $zip_file) {
$zip = new ZipArchive;
if ($zip->open($zip_file, ZipArchive::OVERWRITE) === TRUE) {
foreach ($files as $file) {
if ($file->songType == "1") {
$zip->addFile('assets/songs/' . $file->filePath, $file->filePath);
} else if ($file->songType == "2") {
$zip->addFile('assets/videos/' . $file->filePath, $file->filePath);
}
}
$zip->close();
$_SESSION['cart'] = array();
$_SESSION['cart']['temp'] = array();
$_SESSION['cart']['all'] = array();
return true;
}
else
return false;
}
$temp = 'file.zip';
$pp111 = $temp;
if (createZip($files, $pp111)) {
// header('Content-Type: application/octet-stream');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); //Update modified time to current time.
header("Content-Length: " . filesize($pp111)); //file size
header("Content-Disposition: attachment; filename=\"" . stripslashes($pp111) . "\""); //give the file a name.
ob_clean();
flush();
readfile($pp111); // now start reading the file on your server to start downloading to user's desktop */
// unlink($pp111);
ob_end_flush();
ob_end_clean();
exit();
} else {
exit();
}
?>
【问题讨论】:
-
你可以试试 if (ob_get_level()) {ob_end_clean();} readfile($pp111);看看你得到了什么?
-
不工作仍然同样的问题无法在下载 zip 时在网站上导航。