我对 php 管理的文件下载有问题,其中浏览器不显示文件下载的进度。实际上,浏览器似乎在等待,等待,等待,直到文件完全下载。然后该文件将出现在下载列表中(使用 chrome 和 firefox)。我什至无法用 IE8 下载文件。我希望浏览器显示实际文件大小和下载进度。
奇怪的是,下载甚至在 firebug 中都不可见(如果您粘贴下载 url,网络选项卡中不会出现任何行)。
我怀疑压缩/zlib 有问题,所以我禁用了两者:没有变化。我以相同的结果禁用了输出缓冲。
现场示例可以在这里找到:http: //vps-1108994-11856.manage.myhosting.com/download.php Phpinfo:http: //vps-1108994-11856.manage.myhosting.com/phpinfo.php
代码如下,感谢您的帮助。
<?php
$name = "bac.epub";
$publicname = "bac.epub";
@apache_setenv('no-gzip', 1);
ini_set("zlib.output_compression", "Off");
header("Content-Type: application/epub+zip");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($name));
header("Content-disposition: attachment; filename=" . $publicname) );
ob_end_flush();
flush();
// dump the file and stop the script
$chunksize = 1 * (128 * 1024); // how many bytes per chunk
$size = filesize($name);
if ($size > $chunksize) {
$handle = fopen($name, 'rb');
$buffer = '';
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
echo $buffer;
ob_flush();
flush();
sleep(1);
}
fclose($handle);
} else {
readfile($name);
}
exit;
代码中的休眠是为了确保下载足够长以查看进度。