After my hosting provider upgraded server (Debian) and PHP (from 5.2.6 to 5.3.2) I am having problems with my file download script on our website. Files smaller then 100MB will download fine, but file larger then 100MB will download only as 156 Bytes file … Here is my download script:
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();
}
}
}
I am using Kohana PHP framework. Version 2.3.x.
In the comments, you gave me example links, I tried one, and that 156-byte file I downloaded contained this:
It’s quite clear – PHP ran out of memory. I presume while upgrading they also changed the memory_limit in php.ini. Short-term solution is to change it back to it’s original (higher) value.
For downloading large files, you should look into mod_xsendfile (also available for servers other than apache), that involves setting a special http header, and leaving the work to the webserver instead of php.