Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

http - "Transfer-Encoding: chunked" header in PHP

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("Transfer-Encoding: chunked");
flush();

the browser doesn't want to open the file.

The webpage at ... might be temporarily down or it may have moved permanently to a new web address.

what i need to do for it to work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
";
echo "Hello";
echo "

";
flush();
echo "5
";
echo "World";
echo "
";
flush();
echo "0

";
flush();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...