Search code examples
phpfopenlarge-filesfread

Read/download large file in PHP without running out of memory


How can I read/download a large file in PHP without running into allowed memory bytes exhausted?

I am currently trying:

$content = null;
while (!feof($file['data'])) {
    $content .= fread($file['data'], 8192);
}

header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"" . $file['real_filename'] . "\"");
header("Content-Length: " . $file['length']);

echo $content;

Solution

  • Have you tried this ?

    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=\"" . $file['real_filename'] . "\"");
    header("Content-Length: " . $file['length']);
    
    while (!feof($file['data'])) {
        print fread($file['data'], 8192);
    }