Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP下载文件:在浏览器中显示进度_Php_Progress Bar_Download - Fatal编程技术网

PHP下载文件:在浏览器中显示进度

PHP下载文件:在浏览器中显示进度,php,progress-bar,download,Php,Progress Bar,Download,我的网站上的用户可以下载文件。有时这些文件相当大,我希望用户在浏览器上看到下载进度条 我使用以下代码为用户提供一个文件: header('Content-Type: text/xml'); header('Content-Disposition: attachment; filename='.$fileName); header('Pragma: no-cache'); header('Content-Transfer-Encoding: binary'); header('Pragma: pu

我的网站上的用户可以下载文件。有时这些文件相当大,我希望用户在浏览器上看到下载进度条

我使用以下代码为用户提供一个文件:

header('Content-Type: text/xml');
header('Content-Disposition: attachment; filename='.$fileName);
header('Pragma: no-cache');
header('Content-Transfer-Encoding: binary');
header('Pragma: public');
header('Content-Length: '.filesize($fullFileName));
header("Content-Description: File Transfer");

$fileHandler = fopen($fullFileName, 'r');
while(!feof($fileHandler)){
    echo fread($fileHandler, 2048);
}
fclose($fileHandler);
当我运行这个脚本浏览器(FireFox)冻结一段时间后,我可以看到正在加载的颜色圈,并且只有在“保存/打开”对话框文件出现之后。当我点击“保存”按钮时,文件几乎立即被下载到我的计算机上(而文件相当大——50MB)

我想有一个像这样的下载系统。单击任何链接时,“保存/操作”对话框立即出现。之后,您可以在浏览器中查看下载进度


在浏览器中显示进度条是否有特殊的标题?如何更改代码?

我使用下面的代码,在Firefox中,它确实给了我下载时间和进度:

// send headers first
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-disposition: attachment; filename='.$FileName);
header("Content-Length: ".filesize($Path));
// then use an easy way to flush and end all output buffers
while (@ob_end_flush());
// flush file
readfile($Path);

如果您正确指定
内容长度
,它应该可以工作。我发现代码中唯一有点奇怪的是
内容类型:text/xml
标题。请注意,我使用的是
内容类型:application/octet stream

我使用的是Chrome,我在相关网站上没有看到任何关于下载的特殊内容。在页眉方面,没有任何东西可以发送,以使浏览器显示某种良好的下载进度条。我想这里的问题或多或少是你的电脑,没有别的。@N.B.我不明白为什么在我的网站上,从你点击链接看到“保存文件”对话框的那一刻起,会有一段时间间隔?我认为代码有问题。在显示对话框窗口之前,浏览器似乎要等到整个文件被传输的那一刻。我在使用firefox时遇到了问题,它在通知我开始之前下载了整个文件。我怀疑这和头文件有什么关系(这不可能——毫无意义),要么是你的电脑(硬盘分区满了,要么就是有问题的慢/旧硬盘)要么是firefox的下载机制(同样,我不知道,因为我没有这么多地使用它)。