I need to precisely monitor the amount of data downloaded, this is my test function:
function reply_file($path) {
reply_file_headers($path);
$size = 0;
$in = fopen($path, "r");
$out = fopen("php://output", "w");
while (!feof($in) && !connection_aborted()) {
$buffer = fread($in, 16 * 1024);
fwrite($out, $buffer);
$size += strlen($buffer);
}
fclose($out);
fclose($in);
return $size;
}
I tested it in 3 scenarios:
- I let the download complete
- I cancel the download client
- I physically disable the connection
The first 2 scenarios work as expected and the size returned is correct as to what was received on the other side.
The third scenario has me a bit puzzled, it does return the correct size... eventually, it takes around a minute for it to get to the end of the function, even if it exceeds the script time limit.
If I remove connection_aborted it just goes on to the end as apparently it has no problem writing to the output stream even if there is nobody on the other side.
Now its great that its still working but there is no mention of the blocking feature in PHP documentation. Is this standard behavior or whats happening here exactly?