I am trying to stream live output from some scripts using PHP. There are numerous questions regarding this on StackOverflow. I have followed these answers:
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()?
But, none of them works for me. I am always getting the whole output on command completion. Here is my final code which I have used:
$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>";
Platform: Arch Linux with Apache