我有一个php托管文件下载的问题,浏览器没有显示文件下载的进度。事实上,浏览器似乎在等待和等待,直到文件完全下载。该文件将出现在下载列表中(使用chrome和firefox)。我甚至无法用IE8下载该文件。我希望浏览器显示实际的文件大小和下载进度。php下载不显示在浏览器中的进度
奇怪的是,下载在萤火虫中甚至不可见(如果您粘贴下载网址,网络标签中不会出现任何行)。
我怀疑压缩/ 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;
代码中的睡眠是为了确保下载足够长以查看进度。
这可能与“分块编码”有关。 –
有趣的是,下载进度在我身边显示得很好... – Havelock
Chrome显示进度条,但只有一个不知道文件大小的进度条。 – 472084