note 54295 added to function.readfile
| From: | chrisputnam at gmail dot com | Date: | Wed, 29 Jun 2005 20:44:41 +0000 |
| Subject: | note 54295 added to function.readfile | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-91160@lists.php.net to get a copy of this message | ||
In response to flowbee@gmail.com --
When using the readfile_chunked function noted here with files larger than 10MB or so I am still
having memory errors. It's because the writers have left out the all important flush() after
each read. So this is the proper chunked readfile (which isn't really readfile at all, and
should probably be crossposted to passthru(), fopen(), and popen() just so browsers can find this
information):
<?php
function readfile_chunked($filename,$retbytes=true) {
$chunksize = 1*(1024*1024); // how many bytes per chunk
$buffer = '';
$cnt =0;
// $handle = fopen($filename, 'rb');
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
echo $buffer;
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
?>
All I've added is a flush(); after the echo line. Be sure to include this!
----
Manual Page -- http://www.php.net/manual/en/function.readfile.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54295
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54295&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54295&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54295&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54295&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54295&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54295&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54295&report=yes
Search -- http://master.php.net/manage/user-notes.php