2011-09-29 48 views
0

我使用PHP腳本下載一個20MB的文件如下:PHP文件下載問題:服務器死機了幾分鐘,當用戶取消下載,然後重試

(文件路徑&名在腳本中先前設置)

$fullPath = $filepath.$filename; 

if ($fd = fopen($fullPath, "r")) { 
     // http headers for zip downloads 
     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-type: application/octet-stream"); 
     header("Content-Disposition: attachment; filename=\"".$filename."\""); 
     header("Content-Transfer-Encoding: binary"); 
     header("Content-Length: ".filesize($fullPath)); 
      while(!feof($fd)) { 
      echo(fread($fd, 1024*8)); 
      flush(); 
      if (connection_status()!=0) { 
       fclose($fd); 
       die(); 
      } 
     } 
    } 
fclose($fd); 
exit(); 

它工作正常,如果下載完成,但如果下載用戶取消,而他們點擊鏈接重新下載,服務器是幾分鐘完全沒有反應,然後下載將開始。似乎它正在等待腳本超時...但不是「if(connection_status()!= 0)...」部分應該是該腳本?

任何幫助,非常感謝!先謝謝你!

+0

使用readfile代替@ http://php.net/manual/en/function.readfile.php –

+0

不['ignore_user_abort'](http://php.net/manual/en/function.ignore- user-abort.php)必須與'connection_status'配合使用? –

回答

-1

我想你了工程解決方案一些。嘗試使用readfile()而不是你的fopen/fread循環。

更重要的是,除非有就是爲什麼你需要PHP調解文件傳輸,在所有不使用PHP腳本,只是提供了一個直接鏈接到有問題的文件一個令人信服的理由。

最好的代碼始終是你不必編寫代碼。