php - 在 PHP 中使用实时输出运行进程

标签 php linux apache process real-time

我正在尝试在网页上运行一个进程,该进程将实时返回其输出。例如,如果我运行“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/

相关文章:

php - Google Maps V3 Zoom to Show all markers 不工作

linux - 在 linux 中模拟网络接口(interface)上的传入数据包

php - linux 文件大小

linux - Ubuntu 20.4中的Ncurses与18.04中的版本不兼容

php - 谁能确认 phpMyAdmin AllowNoPassword 适用于 MySQL 数据库?

linux - 域作为 apache 中子域的后备

php - 构建比较字符串相似度的推荐系统

php - 将 HTML header 分离到不同的 PHP 文档中

php - 向 2 行的每个第二个结果添加样式,然后跳过 2 个结果?

apache - .htaccess 文件未将 http ://www. 重定向到 https://www