问题描述
|
如何在执行php脚本之前将标头发送到客户端?
我的脚本需要很长时间才能完成,并且在此期间客户端什么也没收到。
<?
header(\"HTTP/1.1 200 OK\");
header(\"Date: Thu,23 Jun 2011 10:16:31 GMT\");
header(\"Content-Type: application/octet-stream\");
header(\"Content-Length: 12275768\");
header(\"Accept-Ranges: bytes\");
header(\'Content-Disposition: attachment; filename=\"Untitled-1.bmp\"\');
header(\"Content-Transfer-Encoding: binary\");
header(\"Connection: close\");
//Send headers now
//because my php script take many time to finish
//because it downloads a file from a remote server
.
.
.
?>
解决方法
我不确定ѭ1的工作方式以及命令的输出是否以某种方式被缓冲。但是,这是另一个使用ѭ2的示例,它是请求标头字段的流上下文以及带有显式刷新的分块传输编码:
header(\"HTTP/1.1 200 OK\");
header(\"Date: Thu,23 Jun 2011 10:16:31 GMT\");
header(\"Content-Type: application/octet-stream\");
header(\"Accept-Ranges: bytes\");
header(\'Content-Disposition: attachment; filename=\"Untitled-1.bmp\"\');
header(\'Content-Transfer-Encoding: chunked\');
header(\"Connection: close\");
$opts = array(
\'http\'=>array(
\'method\'=>\"GET\",\'header\'=>\"Accept-language: en\\r\\n\" .
\"Cookie: foo=bar\\r\\n\"
)
);
$context = stream_context_create($opts);
$fp = fopen(\'http://www.example.com\',\'r\',false,$context);
while (($buffer = fread($fp,512)) !== false) {
echo sprintf(\"%X\",strlen($buffer)),\"\\r\\n\",$buffer,\"\\r\\n\";
flush();
}
echo \"0\\r\\n\\r\\n\";
flush();
确保不要使用“ 4”或任何其他输出缓冲。
, 在您的php脚本中,创建标头,开始编写html代码,然后刷新,然后继续创建其余的HTML。
, “ 5”功能立即刷新输出缓冲区。您是否正在寻找这样的东西?
, 用ob_start()
输出,然后用something7ѭ;然后再输出一些,然后再按7。