我正在尝试在网页上运行一个进程,该进程将实时返回其输出。例如,如果我运行“ping”进程,它应该在每次返回新行时更新我的页面(现在,当我使用 exec(command, output) 时,我被迫使用 -c 选项并等到进程完成才能看到在我的网页上输出)。是否可以在php中做到这一点?
我也想知道当有人离开页面时终止这种进程的正确方法是什么。在“ping”进程的情况下,我仍然能够看到系统监视器中正在运行的进程(这是有道理的)。
最佳答案
这对我有用:
$cmd = "ping 127.0.0.1";
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a pipe that the child will write to
);
flush();
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
echo "<pre>";
if (is_resource($process)) {
while ($s = fgets($pipes[1])) {
print $s;
flush();
}
}
echo "</pre>";
关于php - 在 PHP 中使用实时输出运行进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1281140/