PHP File (Size) Download Limit?

Hi Chaps,

I’m using readfile to force the download of a file:


$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));

And this works fine, however, I do have some software installation files that could be downloaded (these are in excess of 280Mb).
I have checked php.ini:

memory_limit = 128M
post_max_size = 300M

But Internet Explorer hangs and then crashes.
Is there a way to allow big files to download using this method, or is there another way of forcing the download, without php ‘reading’ the file first?
I’m guessing that the problem lies with the memory_limit being smaller than the file size. Is it a good idea to increase the memory_limit to eg. 280Mb?

Instead of readfile(), output the file in chunks.
There are examples of chunked output at in the comments section.


ini_set(‘memory_limit’, ‘300M’);

My advice – don’t waste the overhead of php or the memory overhead of readfile on something so simple.

Put all the files you want to have forced downloaded in a directory, and set this in your .htaccess for that directory.

<Files *.*>
ForceType applicaton/octet-stream

end of problem.

Then php doesn’t have to get involved at all. (you could also make that .htaccess more complex to target specific filetypes)