2012-09-19 83 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下嘗試它,進度指示器重新出現。

我不認爲進度指示器是越野車 - 它只是內容在它到達之前就被禁止了,它在下載時顯示爲等待狀態。然後,當內容被掃描或批准時,相對於您網站的正常下載速度,內容可能會非常快速地下降。對於足夠大的文件,也許你可以在這個階段看到一個進度指示器。

除了確定這是否是此行爲的真正原因之外,您無能爲力。

相關問題