简体   繁体   中英

How to enable download progress bar in a web browser during file download in PHP script?

I have the following PHP script that outputs a file for download to an end-user:

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);
}

The user can download the file just fine, but when a large file is being downloaded the web browser doesn't show a progress bar with the percentage of the file remaining to download. It only displays the number of MB currently downloaded.

So I'm curious, what shall I change in the header to enable this download progress?

PS. Here's a screenshot:

在此处输入图片说明

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM