note 48683 added to function.readfile
| From: | RobFunk at osu1 dot php dot net | Date: | Tue, 04 Jan 2005 18:31:51 +0000 |
| Subject: | note 48683 added to function.readfile | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-82710@lists.php.net to get a copy of this message | ||
When using readfile() with very large files, it's possible to run into problems due to the
memory_limit setting; apparently readfile() pulls the whole file into memory at once.
One solution is to make sure memory_limit is larger than the largest file you'll use with
readfile(). A better solution is to write a chunking readfile. Here's a simple one that
doesn't exactly conform to the API, but is close enough for most purposes:
function readfile_chunked ($filename) {
$chunksize = 1*(1024*1024); // 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);
}
----
Manual Page -- http://www.php.net/manual/en/function.readfile.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+48683
Delete -- http://master.php.net/manage/user-notes.php?action=delete+48683&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48683&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48683&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48683&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48683&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48683&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+48683&report=yes
Search -- http://master.php.net/manage/user-notes.php