[Tickets #5913] Re: Lower memory usage while downloading files

bugs at bugs.horde.org bugs at bugs.horde.org
Sun Nov 25 23:27:53 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
 Updated By         | olger.diekstra at cardno.com.au
 Summary            | Lower memory usage while downloading files
 Queue              | Gollem
 Version            | HEAD
 Type               | Enhancement
 State              | Feedback
 Priority           | 2. Medium
 Owners             | 
-----------------------------------------------------------------------


olger.diekstra at cardno.com.au (2007-11-25 15:27) wrote:

Hi Chuck,

I posted my code in the initial ticket, but just in case you can't see it
for some reason, here it is again (slightly modified to make it easier to
read/use).
All you'd basically do is call the second function with the filename, and
the file should be send in bits to the browser.

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

Function SendFileToBrowser ($FileName) {
              Header("Content-Type: application/force-download;");
              Header("Content-Disposition: attachment;
filename=\"".$FileName."\"");
              Header("Content-Length: ".FileSize($FileName));
              ReadFileChunked($FileName);
              Exit;
              }

> Btw, you didn't post the code you mentioned yet, but is it really 
> necessary to use chunked reads, or will readfile 
> (http://www.php.net/readfile) or fpassthru do the right thing? In a 
> way fpassthru would be ideal because we can return a stream from the 
> VFS library in some backends, or fake it with a local file.

Did a bit of reading up on fpassthru, and you might want to look at the
first comment on this page:
http://au.php.net/manual/en/function.fpassthru.php.
There's also various other comments on memory usage downloading files on
that page.

Cheers! Olger.



More information about the bugs mailing list