【发布时间】:2022-08-03 14:46:43
【问题描述】:
我正在尝试使用 PHP 流式传输某些脚本的实时输出。 StackOverflow 上有很多关于此的问题。我遵循了这些答案:
PHP reading shell_exec live output
Bash script live output executed from PHP
Run process with realtime output in PHP
Live output to a file with PHP exec()?
但是,它们都不适合我。我总是在命令完成时获得整个输出。这是我使用的最终代码:
$cmd = \"/path/to/command\";
$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>\";
平台:带有 Apache 的 Arch Linux
-
实时过程输出是一个红鲱鱼。为了您自己的调试理智,这是您想要开始工作的general code。问题很可能是 PHP是刷新,但是执行 PHP 的服务器可能正在缓冲。我建议在此答案中通读调整服务器的方法:stackoverflow.com/a/4978642/231316
标签: php