php_nub_qq
php_nub_qq

Reputation: 16055

Reading a file in chunks, weird cycle

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?

Upvotes: 2

Views: 1394

Answers (1)

Binary Alchemist
Binary Alchemist

Reputation: 1620

php://input is a read-only stream. From fread

if the stream is read buffered and it does not represent a plain file, at most one read of up to a number of bytes equal to the chunk size (usually 8192) is made; depending on the previously buffered data, the size of the returned data may be larger than the chunk size.

php://input is a buffered stream which does not represent a plain file. fread is reading one chunk (8192 bytes) at a time.

File size / chucks = read cycle count

93335 / 8192 = 11.4

Upvotes: 3

Related Questions