【发布时间】:2016-03-17 01:03:26
【问题描述】:
我尝试编写一个 php 脚本来创建和下载一个 zip 文件。当我在本地主机上测试脚本时,下载工作正常,但是当它上传到服务器时,出现问题:不是下载 zip 文件,而是文件的内容显示在浏览器中。
有人能指出正确的方向吗?
代码
$zip = new ZipArchive();
$zip->open("$maand/zipfile.zip", ZipArchive::OVERWRITE);
$zip->addFile("$maand/ant/a_nb.txt", 'ant.txt');
$zip->addFile("$maand/lim/l_nb.txt", 'lim.txt');
$zip->addFile("$maand/oos/o_nb.txt", 'oos.txt');
$zip->addFile("$maand/vla/v_nb.txt", 'vla.txt');
$zip->addFile("$maand/wes/w_nb.txt", 'wes.txt');
$zip->close();
$filename = "zipfile.zip";
$filepath = "$maand/";
// headers for zip downloads
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-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
@readfile($filepath.$filename);
【问题讨论】:
-
看这里:stackoverflow.com/questions/8485886/…你必须设置正确的标题。
-
你不需要
ob_end_flush();