2013-12-19 54 views
3

我有一段代碼可以在許多服務器上運行良好。 它用於通過php文件的readfile文件下載文件。通過php函數下載大文件readfile不起作用

但是在一臺特定的服務器中,它不適用於大於25mb的文件。

下面是代碼:

 $sysfile = '/var/www/html/myfile'; 
     if(file_exists($sysfile)) { 
      header('Content-Description: File Transfer'); 
      header('Content-Type: application/octet-stream'); 
      header('Content-Disposition: attachment; filename="mytitle"'); 
      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($sysfile)); 
      ob_clean(); 
      flush(); 
      readfile($sysfile); 
      exit(); 

當我嘗試下載一個文件小於25MB的是沒有問題的,當文件較大,下載的文件是0字節。

我試過了函數read()和file_get_contents,但問題仍然存在。

我的php版本是5.5.3,內存限制設置爲80MB。 錯誤報告已打開,但即使在日誌文件中也沒有顯示錯誤。

+0

什麼是'80mo'? – PeeHaa

+0

首先通過PHP傳遞這些文件的原因是什麼? –

+0

對不起80mo是80MB(兆字節) – Jiwoks

回答

3

下面是完整的解決方案由於witzawitz的答案:

我需要使用ob_end_flush()函數和FREAD();

<?php 
$sysfile = '/var/www/html/myfile'; 
    if(file_exists($sysfile)) { 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename="mytitle"'); 
    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($sysfile)); 
    ob_clean(); 
    ob_end_flush(); 
    $handle = fopen($sysfile, "rb"); 
    while (!feof($handle)) { 
    echo fread($handle, 1000); 
    } 
} 
?> 
3

我最近有同樣的問題。我已經嘗試過不同的標題和其他。 適合我的解決方案。

header("Content-Disposition: attachment; filename=export.zip"); 
header("Content-Length: " . filesize($file)); 
ob_clean(); 
ob_end_flush(); 
readfile($file); 

所以試圖改變沖洗ob_end_flush

+0

它不適用於我:( 我有同樣的結果 – Jiwoks

+0

這是一部分解決方案;)我需要使用fead而不是readfile – Jiwoks