测试代码如下:
<?php
ob_start();
for($i = 1; $i <= 100; $i++){
$sourceName = 'filetoupload' . $i;
$imageArray = array();
$message = "Image $i has been uploaded to the server";
echo $message;
flush();
ob_flush();
usleep(500000);
}
ob_end_flush();
?>
测试结果如下:
在本地服务器上该脚本就可以边运行边输出,可是在内网服务器上运行就是100次循环运行完后才输出;想要的正确结果:
如同在本地服务器上运行结果一样,边运行边输出显示。请问为什么同样的脚本在内网服务器执行就是不同的结果,是不是与php.ini中的哪个配置项有关??? 与哪些配置项有关???望得到大家的帮助,先感谢了
<?php
ob_start();
for($i = 1; $i <= 100; $i++){
$sourceName = 'filetoupload' . $i;
$imageArray = array();
$message = "Image $i has been uploaded to the server";
echo $message;
flush();
ob_flush();
usleep(500000);
}
ob_end_flush();
?>
测试结果如下:
在本地服务器上该脚本就可以边运行边输出,可是在内网服务器上运行就是100次循环运行完后才输出;想要的正确结果:
如同在本地服务器上运行结果一样,边运行边输出显示。请问为什么同样的脚本在内网服务器执行就是不同的结果,是不是与php.ini中的哪个配置项有关??? 与哪些配置项有关???望得到大家的帮助,先感谢了
问一下,你的内网服务器和本地服务器上的PHP版本是一样的吗?
我感觉像这样的情况很多都是PHP版本问题。
ob_flush 支持的PHP版本为(PHP 4 >= 4.2.0, PHP 5)无论是我本机的PHP还是服务器的PHP版本都是支持上述函数的。
http://ca.php.net/manual/en/outcontrol.configuration.php
这个答案我一直半信半疑。。
但也实现不了楼主所说的功能
for($i = 1; $i <= 100; $i++){
$sourceName = 'filetoupload' . $i;
$imageArray = array();
$message = "Image $i has been uploaded to the server";
echo $message;
ob_end_flush();
flush();
usleep(500000);
}
这段代码和我的代码没啥区别,运行结果也是本机服务器是每隔500毫秒输出一次Image $i has been uploaded to the serverImage 其中$i从1一直到100; 内网服务器是100次循环运行完后才输出了所有的内容
比较了上述地址中的implicit_flush、output_buffering和output_handler三项配置是一致的。
本机和内网服务器的PHP.ini中的设置均为:
implicit_flush = Off
output_buffering = Off
;output_handler =
你的服务器是windows系统不 usleep不能工作在Windows 操作系统中
我的本机是windows系统,内网服务器是linux系统
换成sleep(1);试试
试了,在内网服务器(PHP版本:5.1.6 linux系统)下还是循环完后一次性输出结果。
$sourceName = 'filetoupload' . $i;
$imageArray = array();
$message = "Image $i has been uploaded to the server";
echo $message;
if ($id>=100){
exit();
}
$id++;
$msg.="<META HTTP-EQUIV=Refresh CONTENT=0;URL=?id=".$id.">";
echo $msg;
exit();
ob_start();
for($i = 1; $i <= 100; $i++){
ob_end_flush();//关闭缓存
$sourceName = 'filetoupload' . $i;
$imageArray = array();
$message = "Image $i has been uploaded to the server";
echo $message;
flush();
ob_flush();
usleep(500000);
}
ob_end_flush();
?>
<?php
echo "loop start:" . time() ."\n";
for($i = 1; $i <= 100; $i++)
{
sleep(1);
}
echo "loop end:" . time() ."\n" ;
?>
测试环境:
内网服务器(PHP:5.1.6 linux系统)、IE8测试结果:
IE运行该test.php后并不是立马显示页面并输出内容,而是IE的tab标签页左侧的图标一致在转圈圈,IE的进度条一直在走着,直到它们都停止下来后页面才完全显示并输出loop start:1268720273 loop end:1268720373 内容。而测试以下代码:
<?php
echo "loop start:" . time() ."\n";
echo "loop end:" . time() ."\n" ;
?>
如上环境,运行该test.php后,页面立马显示并输出内容。诚恳的请求大家的帮忙,再次感谢,帮自己顶下!
如下代码:<?phpif (ob_get_level() == 0) ob_start();for ($i = 0; $i<10; $i++){ echo "<br> Line to show.";
echo str_pad('',4096)."\n"; ob_flush();
flush();
sleep(2);
}echo "Done.";ob_end_flush();?>
循环外用了一对ob_start();ob_end_flush(); 你又在循环里的开头用了ob_end_flush();//关闭缓存
,你那边这样OK吗?在我这边用IE8访问内网服务器(PHP:5.1.6 linux系统)还是不OK,依然是循环完后一次性输出,而且还会输出一些警告通知信息:
Image 1 has been uploaded to the server
Notice: ob_flush() [ref.outcontrol]: failed to flush buffer. No buffer to flush. in /var/www/html/streaming_serverPush/test.php on line 8
Notice: ob_end_flush() [ref.outcontrol]: failed to delete and flush buffer. No buffer to delete or flush. in /var/www/html/streaming_serverPush/test.php on line 4
这个代码我已经试过了,刚又试了一遍,我把手册中能试的代码也都试了。本机服务器(PHP:5.2.0 Windows系统)是OK的,边运行边输出。 内网服务器(PHP:5.1.6 linux系统)不OK的,运行后一次性输出。按说不应该是脚本的问题,感觉还是服务器之间有差别造成的。但就是不知道问题出在哪里。头大!!!真的很感谢大家帮忙,依然在找答案中...
keep try
===================================
ob_end_clean();
apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);
ini_set('implicit_flush', 1);
ob_start();//开始缓冲数据
for($i = 1; $i <= 100; $i++){
$sourceName = 'filetoupload' . $i;
$imageArray = array();
$message = "Image $i has been uploaded to the server<br/>";
echo $message.str_repeat(" ",1024);//ie有默认的1k buffer
ob_flush();
flush();
usleep(500000);
}
不知道你是不是比我大,但我还是想叫你一声:大哥。
你的这段代码在我之前的那个内网服务器上确实是OK的。
你的这个提醒让我知道了问题的所在:
我之前的内网服务器上的apache配置文件httpd.conf中打开了压缩模块,并且对压缩的类型进行了设置。如下:
#压缩模块
LoadModule deflate_module modules/mod_deflate.so # Controls who can get stuff from this server.
#
Order allow,deny
Allow from all
#压缩的类型有:text/html text/javascript text/css
AddOutputFilterByType DEFLATE text/html text/javascript text/css
</Directory>这样当我服务器向客户端输出内容时就会先缓冲起来,等完成后一次性输出。这就造成了我上述的问题。真的感谢,太感谢了。