【问题标题】:How to enable download progress bar in a web browser during file download in PHP script?如何在 PHP 脚本中下载文件期间在 Web 浏览器中启用下载进度条?
【发布时间】:2015-04-11 04:35:46
【问题描述】:

我有以下 PHP 脚本输出文件以供最终用户下载:

session_write_close();
ob_end_clean();
set_time_limit(0);

if($file = fopen($path, 'rb'))
{
    header("Cache-Control: no-cache");
    header("Pragma: no-cache");
    header("Accept-Ranges: bytes");
    header("Content-length: ".(string)(filesize($path)));
    header("Content-Disposition: attachment; filename=\"".basename($path)."\"");
    header("Content-type: application/octet-stream");
    header("Content-Transfer-Encoding: binary");
    header("Connection: close");

    fpassthru($file);
    fclose($file);
}

用户可以很好地下载文件,但是当下载大文件时,网络浏览器不会显示进度条以及剩余要下载的文件百分比。它只显示当前下载的 MB 数。

所以我很好奇,我应该在标题中更改什么以启用此下载进度?

PS。截图如下:

【问题讨论】:

    标签: php http download http-headers


    【解决方案1】:

    假设它是 gzip 并且与不知道实际内容长度相关...也许添加此标头?

    header("accept-encoding: gzip;q=0,deflate;q=0");
    

    【讨论】:

    • 谢谢,但它并没有改变它。我也试过Accept-encoding: gzip; deflate;
    • 你认为它与 gzip 有关吗?查看请求是否可以查看是否正在使用压缩?可以尝试在 Web 服务器上将其关闭以进行测试。有问题的网络服务器是什么?
    • 服务器是Apache/Linux 3.12.35.1418868052。如何确定它是否使用 gzip?还有什么方法可以通过 php 转储标头?
    • Apache 的mod_deflate。通常使用SetEnv 调用禁用。或者假设注释掉模块并重新启动。您是否在询问 PHP 的 headers_list() - 我建议使用 firebug/fiddler/wireshark 等以获得更好的洞察力。
    猜你喜欢
    • 1970-01-01
    • 2014-11-18
    • 2022-12-30
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 2017-02-15
    • 2014-10-27
    • 2018-12-05
    相关资源
    最近更新 更多