【发布时间】:2011-12-05 09:20:36
【问题描述】:
我的网站上运行了以下代码。我唯一遇到的问题是它在服务器上制作了一个 zip 文件,然后用户下载。
我想知道我应该怎么做才能“即时”生成 zip 文件而不先转储到服务器磁盘上。我还想让用户暂停/恢复下载。
//function for zip
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
//create the object
$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
//add each files of $file_name array to archive
foreach($file_names as $files)
{
$zip->addFile($file_path.str_replace('./','',$files),translit($files).".mp3");
}
$zip->close();
//then send the headers to foce download the zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
exit;
}
【问题讨论】:
-
php 手册中没有提到 file() 方法,因此如果该帖子确实有效,则可能无法保证继续有效。