From: psurmano at ucalgary dot ca
Operating system: Redhat 8.0
PHP version: 4.3.3
PHP Bug Type: Output Control
Bug description: fread() reads entire file to memory

Description:
------------
When the following code is run in a browser on a very large file php will
first read in the entire file into memory and only afterwards will it
display a download dialog in the browser. This is even though I'm reading
in 8192 bytes at a time.

On a large file this results in a delay of several seconds.

Using readfile() doesn't have this problem but it is unacceptable since I
will need to add code to throttle download speed.

Reproduce code:
---------------
<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=largeFile");
header("Content-Length: " . filesize("largeFile"));

$handle = fopen ("largeFile", "rb");
do {
$data = fread($handle, 8192);
if (strlen($data) == 0) {
break;
}
echo $data;
} while(true);
fclose ($handle);
?>


Expected result:
----------------
PHP should first show the download dialog and then start reading the file.
This is what happens with readfile()

Actual result:
--------------
The file will first get read entirely into memory resulting in a large
delay. Only then is the user prompted with a download dialog.

--
Edit bug report at [url]http://bugs.php.net/?id=25523&edit=1[/url]
--
Try a CVS snapshot (php4): [url]http://bugs.php.net/fix.php?id=25523&r=trysnapshot4[/url]
Try a CVS snapshot (php5): [url]http://bugs.php.net/fix.php?id=25523&r=trysnapshot5[/url]
Fixed in CVS: [url]http://bugs.php.net/fix.php?id=25523&r=fixedcvs[/url]
Fixed in release: [url]http://bugs.php.net/fix.php?id=25523&r=alreadyfixed[/url]
Need backtrace: [url]http://bugs.php.net/fix.php?id=25523&r=needtrace[/url]
Try newer version: [url]http://bugs.php.net/fix.php?id=25523&r=oldversion[/url]
Not developer issue: [url]http://bugs.php.net/fix.php?id=25523&r=support[/url]
Expected behavior: [url]http://bugs.php.net/fix.php?id=25523&r=notwrong[/url]
Not enough info: [url]http://bugs.php.net/fix.php?id=25523&r=notenoughinfo[/url]
Submitted twice: [url]http://bugs.php.net/fix.php?id=25523&r=submittedtwice[/url]
register_globals: [url]http://bugs.php.net/fix.php?id=25523&r=globals[/url]
PHP 3 support discontinued: [url]http://bugs.php.net/fix.php?id=25523&r=php3[/url]
Daylight Savings: [url]http://bugs.php.net/fix.php?id=25523&r=dst[/url]
IIS Stability: [url]http://bugs.php.net/fix.php?id=25523&r=isapi[/url]
Install GNU Sed: [url]http://bugs.php.net/fix.php?id=25523&r=gnused[/url]