我有一個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