note 78406 added to function.fread
| From: | matt at matt-darby dot com | Date: | Wed, 10 Oct 2007 17:32:42 +0000 |
| Subject: | note 78406 added to function.fread | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131278@lists.php.net to get a copy of this message | ||
I thought I had an issue where fread() would fail on files > 30M in size. I tried a
file_get_contents() method with the same results. The issue was not reading the file, but echoing
its data back to the browser.
Basically, you need to split up the filedata into manageable chunks before firing it off to the
browser:
<?php
$total = filesize($filepath);
$blocksize = (2 << 20); //2M chunks
$sent = 0;
$handle = fopen($filepath, "r");
// Push headers that tell what kind of file is coming down the pike
header('Content-type: '.$content_type);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-length: '.$filesize * 1024);
// Now we need to loop through the file and echo out chunks of file data
// Dumping the whole file fails at > 30M!
while($sent < $total){
echo fread($handle, $blocksize);
$sent += $blocksize;
}
exit(0);
?>
Hope this helps someone!
----
Server IP: 208.69.120.35
Probable Submitter: 24.123.63.138
----
Manual Page -- http://www.php.net/manual/en/function.fread.php
Edit -- https://master.php.net/note/edit/78406
Del: integrated -- https://master.php.net/note/delete/78406/integrated
Del: useless -- https://master.php.net/note/delete/78406/useless
Del: bad code -- https://master.php.net/note/delete/78406/bad+code
Del: spam -- https://master.php.net/note/delete/78406/spam
Del: non-english -- https://master.php.net/note/delete/78406/non-english
Del: in docs -- https://master.php.net/note/delete/78406/in+docs
Del: other reasons-- https://master.php.net/note/delete/78406
Reject -- https://master.php.net/note/reject/78406
Search -- https://master.php.net/manage/user-notes.php