php处理数据网页输出导出exec文件,javascript – PHP:在Web浏览器中输出system/Shell_exec命令输出...

嗯,来自网络浏览器的奇怪行为.我正在使用此代码:

ob_end_flush();

ini_set("output_buffering", "0");

ob_implicit_flush(true);

function pingtest()

{

$proc = popen("ping -c 5 google.com", 'r');

while (!feof($proc))

{

echo "[".date("i:s")."] ".fread($proc, 4096);

}

}

?>

 
 

Immediate output:

pingtest();

?>

在浏览器中,在收到所有字节后显示内容.

但是,内容实际上是按时交付的,请做这个测试:

wget -O - -q "http://localhost/ping.php"

您将看到响应由php& apache2准时.

我暂时在长期任务中使用这种执行,但使用更复杂的解决方案:

>接口的html文件

>运行长任务的php文件

>使用EventSource对象连接html界面和php长执行(在html5上可用)

接口(test.html)

Simple EventSource example

function eventsourcetest() {

var ta = document.getElementById('output');

var source = new EventSource('test.php');

source.addEventListener('message', function(e) {

if (e.data !== '') {

ta.value += e.data + '\n';

}

}, false);

source.addEventListener('error', function(e) {

source.close();

}, false);

}

Output:

ping google.com

服务器端组件(test.php)

ob_end_flush();

ini_set("output_buffering", "0");

ob_implicit_flush(true);

header('Content-Type: text/event-stream');

header('Cache-Control: no-cache');

function echoEvent($datatext) {

echo "data: ".implode("\ndata: ", explode("\n", $datatext))."\n\n";

}

echoEvent("Start!");

$proc = popen("ping -c 5 google.com", 'r');

while (!feof($proc)) {

echoEvent(fread($proc, 4096));

}

echoEvent("Finish!");

将两个文件放在Web服务器上的一个位置并输入test.html,我认为这是您从一开始就要查找的内容.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值