I've crafted my own version of simple solution, for sending large files in PHP. It uses file chunks. This solution is proted from various sources and I copy-pasted following code to implement "chunking":
$handle = fopen($filename, 'rb');
while (!feof($handle))
{
print(@fread($handle, $chunksize));
ob_flush();
flush();
}
fclose($handle);
Since it uses print
to send each chunk to the browser, it is very sensitive to character encoding, in which PHP script is encoded. For example, I noticed, that when script is saved in ANSI
, downloaded files are corrupted. Only, if I save & upload script encoded in utf-8
, file are fine.
Is there a better function, than print
to do the same (send piece of file to browser), that would be independend of script file encoding -- because, for example, would force binary transfer to browser?