2010-01-19 89 views
0

當我下載原始壓縮文件時,它工作正常,但是當我使用下面的頭文件和它下載它時,它不起作用。我知道最好採取這種方式,並告訴瀏覽器如何處理文件,而不是讓它保留在瀏覽器中,但我無法得到這個工作,所以我很想使用header()。文件下載不工作

  $path = $this->tru->config->get('root.path').'/Digital Version of Book for Web.zip'; 

      set_time_limit(0); 
      header("Cache-Control: public"); 
      header("Content-Description: File Transfer"); 
      header('Content-Type: application/zip'); 
      header("Content-Transfer-Encoding: binary"); 
      header('Content-Disposition: attachment; filename="NewFileName.zip"'); 
      header('Content-Length: ' . filesize($path)); 

      $f = fopen($path, 'rb'); 
      fpassthru($f); 
      fclose($f); 

編輯:

對不起,我的意思是它不工作是在一個zip格式的文件下載(全部9.3 MB),但我無法解包拉鍊,因爲這是無效的。

+0

「它不起作用」太含糊。請解釋什麼是錯的。文件是否損壞? – 2010-01-19 15:00:04

+0

看看這個:http://stackoverflow.com/questions/2088267/download-of-zip-file-runs-a-corrupted-file-php/2088331#2088331 – Gumbo 2010-01-19 21:16:34

回答

1

使用記事本或其他文本編輯器查看ZIP文件。檢查前幾行是否有PHP錯誤信息搞砸了文件。由於腳本處於安全模式,它可能是「頭文件已發送」消息或set_time_limit()調用引發錯誤。

0

嘗試使用readfile()PHP Manual提供了一個示例。

$file = 'monkey.gif'; 

if (file_exists($file)) { 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename='.basename($file)); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Pragma: public'); 
    header('Content-Length: ' . filesize($file)); 
    ob_clean(); 
    flush(); 
    readfile($file); 
    exit; 
} 
+0

我使用相同的例子,但其不在我的'localhost'上工作。我有'$ file =「../ files/myfile.pptx」'。這有什麼問題嗎? – 2013-10-01 14:50:32