2012-09-19 79 views
2

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

奇怪的是,下载在萤火虫中甚至不可见(如果您粘贴下载网址,网络标签中不会出现任何行)。

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

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

+0

这可能与“分块编码”有关。 –

+0

有趣的是,下载进度在我身边显示得很好... – Havelock

+0

Chrome显示进度条,但只有一个不知道文件大小的进度条。 – 472084

回答

1

保持它,真的很简单。

<?php 

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

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

这是你真正需要的。

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

我已经修改了你的代码弗朗西斯。现在它可以... :)

0

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

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

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

相关问题