note 55519 added to function.fpassthru
| From: | jonathan at corporacionlinux dot cl | Date: | Fri, 05 Aug 2005 15:58:30 +0000 |
| Subject: | note 55519 added to function.fpassthru | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93138@lists.php.net to get a copy of this message | ||
While trying to "passthru" a file to the browser via PHP, but using the FEOF loop, the
script tried to buffer the entire file before passing it to the browser. This is my original script.
When calling it with a 15M PHP memory limit and a 16M file, apache killed the script.
<?php
$name = $tempDir . $_GET["file"];
$fd = fopen($name, 'rb');
if($fd == false)
die("<font color=red>ERROR: File not found.</font>");
// send the right headers
header("Cache-Control: ");// leave blank to avoid IE errors
header("Pragma: ");// leave blank to avoid IE errors
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . $_GET["file"] .
"\"");
header("Content-length:".(string)(filesize($name)));
sleep(1);
session_write_close();
ob_flush();
flush();
while(!feof($fd)) {
$buffer = fread($fd, 2048);
print $buffer;
}
fclose ($fd);
exit;
?>
Apache error log read:
Allowed memory size of 15728640 bytes exhausted (tried to allocate 10240 bytes)
I tried everything, including a flush() inside the loop. But the solution was forcing the flush
other way:
<?php
$buffer = fread($fd, 32 * 1024);
?>
voila... works just fine for me.
----
Manual Page -- http://www.php.net/manual/en/function.fpassthru.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55519
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55519&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55519&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55519&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55519&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55519&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55519&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55519&report=yes
Search -- http://master.php.net/manage/user-notes.php