php下载不显示浏览器的进度

时间:2012-09-19 14:52:42

标签: php

我有一个php托管文件下载的问题,浏览器没有显示文件下载的进度。事实上,浏览器似乎在等待,等待和等待,直到文件完全下载。然后该文件将出现在下载列表中(使用chrome和firefox)。我甚至无法用IE8下载该文件。我希望浏览器显示实际文件大小和下载进度。

奇怪的是,在firebug中甚至看不到下载(如果粘贴下载URL,网络选项卡中不会显示任何行)。

我怀疑压缩/ 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;

代码中的睡眠是为了确保下载足够长以查看进度。

3 个答案:

答案 0 :(得分:1)

保持它,真的,真的,简单。

<?php

header("Content-Type: application/epub+zip");
header("Content-disposition: attachment; filename=" . $publicname) );

if(!readfile($name))    
    echo 'Error!';
?>

这就是你真正需要的。

答案 1 :(得分:1)

            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($file_path));
            header("Content-disposition: attachment; filename=" . $local_file_name);

            // dump the file and stop the script
            $chunksize = 128 * 1024; // how many bytes per chunk (128 KB)
            $size = filesize($file_path);
            if ($size > $chunksize)
            {
                $handle = fopen($file_path, 'rb');
                $buffer = '';
                while (!feof($handle))
                {
                    $buffer = fread($handle, $chunksize);
                    echo $buffer;
                    flush();
                    sleep(1);
                }
                fclose($handle);
            }
            else
            {
                readfile($file_path);
            }

我修改了你的代码Francis。现在它起作用了...... :)

答案 2 :(得分:0)

这可能是由您和远程站点之间的防火墙或某种代理引起的。我正在努力解决相同的问题 - 禁用gzip,刷新缓冲区等,直到我在Web VPN下尝试它并且进度指示器重新出现。

我不认为进度指示器是错误的 - 只是内容在它到达之前被禁止,这在下载中显示为等待状态。然后,当扫描或批准内容时,它可能会相对于您网站的正常下载速度非常快地下降。对于足够大的文件,也许您可​​以在此阶段看到进度指示器。

除了确定这是否是造成这种行为的真正原因之外,你无能为力。