“Transfer-Encoding: chunked” header in PHP

前端 未结 4 1591
小鲜肉
小鲜肉 2020-12-03 18:40

i want to add Transfer-Encoding: chunked header to the file that i\'m outputing (its just generated plain text), but when i add:

header(\"Transf         


        
4条回答
  •  广开言路
    2020-12-03 19:15

    You need to send the Content-Length with every chunk you send. Look at Wikipedia for a first impression, how a chunked encoding looks like. Its not that trivial and in many cases its oversized.

    Update: First you send the headers, because they must always send before any content (also with chunked encoding). Then you send (for every chunk) the size (in hexadecimal) followed by the content. Remember flush() after every chunk. At last you must send a zero-size chunk to make sure, that the connection get closed properly.

    Its not tested, but something like this

    header("Transfer-Encoding: chunked");
    echo "5\r\n";
    echo "Hello";
    echo "\r\n\r\n";
    flush();
    echo "5\r\n";
    echo "World";
    echo "\r\n";
    flush();
    echo "0\r\n\r\n";
    flush();
    

提交回复
热议问题