php 输出 继续执行,使用PHP中的实时输出运行进程

本文介绍了如何在PHP中使用shell命令`rsync`进行实时输出,并展示了如何禁用输出缓冲以确保命令执行过程中的每条输出都能立即显示。提供的代码示例演示了如何设置HTTP头、关闭输出缓冲以及使用`system()`函数执行命令。同时,文章提到了这种方法可能在某些服务器上不起作用,并鼓励读者检查PHP配置以确定问题所在。
摘要由CSDN通过智能技术生成

这是显示shell命令的实时输出的好方法:

header("Content-type: text/plain");

// tell php to automatically flush after every output

// including lines of output produced by shell commands

disable_ob();

$command = 'rsync -avz /your/directory1 /your/directory2';

system($command);

您将需要此功能来防止输出缓冲:

function disable_ob() {

// Turn off output buffering

ini_set('output_buffering', 'off');

// Turn off PHP output compression

ini_set('zlib.output_compression', false);

// Implicitly flush the buffer(s)

ini_set('implicit_flush', true);

ob_implicit_flush(true);

// Clear, and turn off output buffering

while (ob_get_level() > 0) {

// Get the curent level

$level = ob_get_level();

// End the buffering

ob_end_clean();

// If the current level has not changed, abort

if (ob_get_level() == $level) break;

}

// Disable apache output buffering/compression

if (function_exists('apache_setenv')) {

apache_setenv('no-gzip', '1');

apache_setenv('dont-vary', '1');

}

}

它不适用于我尝试过的每一台服务器,我希望我可以提供一些关于你在php配置中寻找什么的建议,以确定你是否应该试着让你的头发试图让这种行为发挥作用在你的服务器上!其他人都知道吗?

这是普通PHP中的一个虚拟示例:

header("Content-type: text/plain");

disable_ob();

for($i=0;$i<10;$i++)

{

echo $i . "\n";

usleep(300000);

}

我希望这能帮助那些在谷歌上搜索的人。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值