Okay so I am trying to read a 91kb file in chunks of 1048576
bytes ( 1 MB ) which is supposed to read the file instantaneously in one chunk, however this is not what I'm getting
public function uploadTmpFileFromXHRStream(){
header('Content-type: text/html; charset=utf-8');
function output($val)
{
echo $val;
flush();
ob_flush();
}
$in = fopen('php://input', 'r');
$tmpFileId = uniqid(null,true);
$out = fopen($tmpFileId.'_'.$_SERVER['HTTP_X_MFXHRFILEUPLOAD'], 'x');
while($data = fread($in,1048576)){
fwrite($out, $data);
output(1);
sleep(2);
}
}
On the other side I have set up javascript to listen on xhr.readystatechange
at readyState==3
and simply just log output to the console. What I have in the console is this:
200 OK 24,02s
1
11
111
1111
11111
111111
1111111
11111111
111111111
1111111111
11111111111
111111111111
There're 12 iterations in the while
loop, the exact file size from php://input
is 93335. I'm pretty confused, why is this happening?