note 54295 modified in function.readfile by sp
| From: | sp@php.net | Date: | Wed, 15 Feb 2006 17:39:46 +0000 |
| Subject: | note 54295 modified in function.readfile by sp | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-103782@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;
ob_flush();
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!
--was--
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!
http://php.net/manual/en/function.readfile.php