php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #25523 fread() reads entire file to memory
Submitted: 2003-09-13 01:24 UTC Modified: 2003-09-13 05:10 UTC
From: psurmano at ucalgary dot ca Assigned:
Status: Not a bug Package: Output Control
PHP Version: 4.3.3 OS: Redhat 8.0
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: psurmano at ucalgary dot ca
New email:
PHP Version: OS:

 

 [2003-09-13 01:24 UTC] psurmano at ucalgary dot ca
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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-09-13 03:34 UTC] wez@php.net
Turn off output buffering....
 [2003-09-13 05:10 UTC] phildriscoll@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Try a flush() after the echo.  
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 14:01:30 2024 UTC