I want to do a binary read on files to force download of the files, and I was wondering if it's possible to do this without having to read the entire file into memory. So for instance, the following code I would assume would read the entire contents into memory
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
Is there a way around this?
At first, I thought doing a partial read in a loop might get around this (such as this)
while (!feof($handle)) {
echo fread($handle, 8192);
}
but then I thought it's still probably keeping it all in memory until the end of the script at which point it's output.
Maybe this would work?
while (!feof($handle)) {
echo fread($handle, 8192);
ob_flush();
}
Does anyone have any thoughts on this (to prevent 20 MB of memory from being consumed each time a 20MB file is downloaded)?
Thanks for any suggestions.