[Tickets #5913] Fix for php memory_limit
bugs at bugs.horde.org
bugs at bugs.horde.org
Thu Nov 22 00:12:25 UTC 2007
DO NOT REPLY TO THIS MESSAGE. THIS EMAIL ADDRESS IS NOT MONITORED.
Ticket URL: http://bugs.horde.org/ticket/?id=5913
-----------------------------------------------------------------------
Ticket | 5913
Created By | olger.diekstra at cardno.com.au
Summary | Fix for php memory_limit
Queue | Gollem
Version | 1.0.3
Type | Enhancement
State | New
Priority | 3. High
Owners |
-----------------------------------------------------------------------
olger.diekstra at cardno.com.au (2007-11-21 16:12) wrote:
Gollem (probably Horde as an application entirely) needs quite a memory
footprint (PHP memory_limit) for downloading mail attachments or
downloading files from Gollem.
I haven't tested this problem in the latest and greatest of Horde, but all
tickets and forums I've seen indicate this is no different in teh current
release.
The problem comes from the fact that the entire file that is about to be
downloaded is read entirely into memory (on the server) before it is pushed
out to the browser. This basically means that a 200MB file stored in Gollem
needs a PHP memory_limit of at least 200MB to be able to download it. If
one foreach is used on the array storing the file in memory, the
memory_limit doubles.
To get around this problem is easy, push out the file in chunks, instead
of all at once. I have no idea how Gollem (or Horde) pushes out a file to a
browser, but I'd imagen it would be similar as I do below.
I think the below code is easy enough to read without me blabbering on
about it... Its code I use myself for a similar sort of function. And, as
far as I'm concerned, is available for Horde to use. I've basically created
the code myself using code templates for ideas I found on various places on
the net.
Using this code, PHP's memory_limit can be left at default 8MB, and any
sized file can be downloaded.
Questions? Do ask!
Function ReadFileChunked ($FileName) {
$chunksize = (102400); // how many bytes per chunk
$buffer = '';
$handle = fopen($FileName, 'rb');
if ($handle === false) { return false; }
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
print $buffer;
}
return fclose($handle);
}
{ $File["File"] = "200MBfiletobedownloaded.zip";
$File["Size"] = FileSize($File["File"]);
Header("Content-Type: application/force-download;");
Header("Content-Disposition: attachment;
filename=\"".$File["File"]."\"");
Header("Content-Length: ".$File["Size"]);
ReadFileChunked($File["File"]);
Exit;
}
More information about the bugs
mailing list