2010-03-26 33 views
1

使用下面的代碼,小文件服務正常,然而很大(見800MB及以上)導致空文件!服務文件(800MB)產生一個空文件

我需要用Apache來做些什麼來解決這個問題嗎?

<?php 


    class Model_Download { 


     function __construct($path, $file_name) { 


$this->full_path = $path.$file_name; 
    } 


    public function execute() { 

     if ($fd = fopen ($this->full_path, "r")) { 
      $fsize  = filesize($this->full_path); 
      $path_parts = pathinfo($this->full_path); 
      $ext  = strtolower($path_parts["extension"]); 

      switch ($ext) { 
       case "pdf": 
        header("Content-type: application/pdf"); // add here more headers for diff. extensions 
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download 
       break; 
       default; 
        header("Content-type: application/octet-stream"); 
        header("Content-Disposition: filename=\"".$path_parts["basename"]."\""); 
       break; 
      } 

      header("Content-length: $fsize"); 
      header("Cache-control: private"); //use this to open files directly 

      while(!feof($fd)) { 
       $buffer = fread($fd, 2048); 
       echo $buffer; 
      } 
     } 
     fclose ($fd); 
     exit; 
    } 


} 

編輯:如果我使用

  fpassthru($fd); exit; 

相反,我得到下面寫了一個文件中:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 786032641 bytes) in /Users/aaron/Sites/com/library/Model/Download.php on line <i>44 
+0

中有什麼一旦他們被下載的文件?嘗試用文本編輯器打開一個。 –

+0

它似乎在傳輸該數據嗎?用whireshark嗅探進一步診斷。 – Caissy

+0

它導致一個空文件,在文本編輯器中打開它顯示其寫入一個空文件。 我會馬上看看whireshark。 – azz0r

回答

0

我已經使用mod_xsendfile的路線,我已經寫了一具有以下獨立腳本:

 $the_clip = 'files/Clip/'.$clip_bought->file_url; 
     header('X-Sendfile: '.$the_clip); 
     header('Content-Type: application/octet-stream'); 
     header('Content-Disposition: attachment; file="'.$the_clip.'"'); 
     exit; 

它提供文件,下載工作,但是它將文件作爲download.php(腳本的文件名)提供,而不是實際文件的名稱。

有沒有人知道一個頭,可以讓你服務的文件名,因爲你想嗎?

感謝

編輯:

使用實時頭,我可以看到它這樣做的:

HTTP/1.1 200 OK 
Date: Tue, 06 Apr 2010 10:42:28 GMT 
Server: Apache/2.2.9 (Debian) DAV/2 SVN/1.5.1 PHP/5.2.6-1+lenny6 with Suhosin-Patch 
X-Powered-By: PHP/5.2.6-1+lenny6 
Content-Disposition: attachment; file="movie.wmv" 
Last-Modified: Fri, 02 Apr 2010 13:09:32 GMT 
Content-Length: 1107113956 
Etag: "d8084-41fd37e4-48340b0ab3b00" 
Keep-Alive: timeout=15, max=100 
Connection: Keep-Alive 
Content-Type: application/octet-stream 

...但仍擔任該文件的download.php

+0

經過研究,第4行的$ the_clip現在是文件的名稱,而不是它的路徑,但它仍然作爲download.php,我猜它肯定是一個mod_xsendfile的問題? – azz0r