2012-01-13 67 views
0

在我的托管服务提供商升级服务器(Debian)和PHP(从5.2.6到5.3.2)后,我在我们的网站上遇到了我的文件下载脚本问题。小则100MB的文件将下载好的,但较大的文件,然后将100MB的下载仅156个字节的文件......这是我的下载脚本:PHP强制下载 - 大于100MB的文件将只下载少量内容

class Download_Controller extends Website_Controller 
{ 

    public function index() 
    { 
     if (isset($_GET['file'])) { 
      $file  = $_GET['file']; 
      $filORM = ORM::factory('file')->where('filename', $file)->find(); 

      if ($filORM->loaded and $filORM->deleted=='N' and file_exists(APPPATH.'downloads/'.$file)) { 
      //we can serve file download 
      $this->auto_render = false; 

      $filORM->counter = $filORM->counter + 1; 
      $filORM->save(); 

      $dl = ORM::factory('download'); 
      $dl->download_file_id = $filORM->id; 
      $dl->created = time(); 
      $dl->country_id = $this->country->id; 
      $dl->ip = $this->_getRealIpAddr(); 
      $dl->browser = Kohana::user_agent('browser'); 
      $dl->version = Kohana::user_agent('version'); 
      $dl->platform = Kohana::user_agent('platform'); 
      $dl->save(); 

      return download::force(APPPATH.'downloads/'.$file); 
      } 
      else { 
      $this->download_error(); 
      } 

     } 
     else { 
      //else here we load download center UI 
      $this->section(); 
     } 
    } 
} 

我使用Kohana的PHP框架。版本2.3.x.

+0

你有访问服务器吗?或者它是一个托管服务器? – Stony 2012-01-13 09:39:44

+0

我有ssh访问权限。 – 2012-01-13 09:40:56

+0

这些156字节文件的内容是什么?有什么特别的? – Maerlyn 2012-01-13 09:43:27

回答

2

在评论,你给我的示例链接,我试过一次,那156个字节的文件我下载的内容如下:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 141637633 bytes) in /home/www-data/system/helpers/download.php on line 93

这很清楚 - PHP耗尽内存。我认为在升级时他们也改变了php.ini中的memory_limit。短期解决方案是将其更改回原始(更高)值。

对于下载大文件,您应该查看mod_xsendfile(也可用于apache以外的服务器),包括设置一个特殊的http头,并将工作留在web服务器而不是php。

0

如果Kohana的download::force()的工作方式与其他任何框架相同 - PHP无法或不允许在内存中保存超过100MB的数据。

+0

它在服务器升级之前完美地工作......?任何解决方法? – 2012-01-13 09:48:21

0

我不知道download::force()的代码是什么,但我认为它将整个文件加载到内存中,并且PHP停止执行,出现错误,如Allowed memory size is exhausted。您需要通过小块检查客户端是否中止连接来加载和输出文件。

更新

你的文件中包含Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 141637633 bytes) in /home/www-data/system/helpers/download.php on line 93。所以,就像我写的那样,以小块输出它。

0

你可以尝试readfile(APPPATH.'downloads/'.$file)然后exit()的情况下直接return,那么你将不会被绑定到内存问题了