2013-12-19 55 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