2

我对 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;

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

4

3 回答 3

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。现在它可以工作了...... :)

于 2013-05-09T07:54:46.507 回答
1

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

<?php

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

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

这就是你真正需要的。

于 2012-09-19T15:02:43.617 回答
0

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

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

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

于 2015-04-13T23:49:07.103 回答