Run Process With Realtime Output in PHP

Run process with realtime output in PHP

This worked for me:

$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>";

running shell command in php with realtime output

Disable output buffering using ob_end_flush, and start the process using proc_open instead of shell_exec, so you have full control over the file descriptors and don't need to wait for the process to finish to get the output. When you have started the process using proc_open, read from its STDOUT and STDERR and output it to the browser, possibly with a flush to assure your output is not kept in any buffer.

Read realtime whole output in stream

use fread() to replace fgets().

Run process in real time

Use pcntl_fork() and pcntl_exec() like this

printing process output in realtime

some web browsers buffer the first x bytes before they start to render a page, under certain conditions.

try just outputting lots of whitespace first

Execute shell, get and display result realtime using PHP

PHP always wait until script is done then displaying it.

In this case, it's not PHP's fault; it's because Python fully buffers its standard output if that goes to a pipe. Fortunately, Python has the handy option -u to disable stdout and stderr buffering, so using

    $handle = popen('python -u folder\\a.py', 'r');

solves the problem. See also Disable output buffering.

Run process with realtime output in PHP

This worked for me:

$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>";


Related Topics



Leave a reply



Submit