“Transfer-Encoding:chunked” PHP中的头文件 [英] "Transfer-Encoding: chunked" header in PHP
问题描述
我想将 Transfer-Encoding:chunked
标题添加到我输出的文件中(它刚生成的纯文本),但是当我添加时:
标题(Transfer-Encoding:chunked);
flush();
浏览器不想打开文件。
在...处的网页可能是暂时关闭的
,或者它可能已将
永久移动到新的网页地址。
我需要为它做些什么?
您需要发送每个块的 Content-Length
。查看维基百科,了解第一印象,分块编码的外观。它不是那么简单,而且在很多情况下它的尺寸过大。
更新:
首先发送标题,因为它们必须始终在任何内容之前发送(也与分块编码)。然后你发送(每个块)大小(以十六进制),然后是内容。在每个块后记住 flush()
。最后,你必须发送一个零大小的块,以确保连接正常关闭。
未经测试,但类似这样
头(Transfer-Encoding:chunked);
回声5 \r\\\
;
回显你好;
回显\r\\\
\r\\\
;
flush();
回声5 \r\\\
;
回声世界;
回声\r\\\
;
flush();
回显0 \r\\\
\r\\\
;
flush();
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?
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();
这篇关于“Transfer-Encoding:chunked” PHP中的头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!