php shell_exec with realtime updating

I have this shell program that I want to execute by php. The problem is that it can potentially take a long time, and as of that I need it to have real-time updating to the user's browser.

I read that I may need to use popen() to do that, but I am sort of (ok, I really am :P) a PHP noob and can't figure out how I may be able to do it.

Would appreciate any help!

if( ($fp = popen("your command", "r")) ) {
    while( !feof($fp) ){
        echo fread($fp, 1024);
        flush(); // you have to flush buffer
    }
    fclose($fp);
}

if( ($fp = popen("your command", "r")) ) { while( !feof($fp) ){ echo fread($fp, 1024); flush(); // you have to flush buffer } fclose($fp); }. I have PHP (CGI) and Apache. I also shell_exec() shell scripts which use PHP CLI. This combination destroys the string value returned from the call. I get binary garbage. Shell scripts that start with #!/usr/bin/bash return their output properly. A solution is to force a clean environment. PHP CLI no longer had the CGI environment variables to choke on.

there are two possible behaviors:

  1. Non Block, where you need to do something else between flushs (@GameBit show how to do it).

  2. With Block, where you wait until the called command finish, in this case look passthru function

If I do shell_exec() - we can't see output for ping (if we use ping without /​8370628/php-shell-exec-with-realtime-updating in yii2 MVC model. by php : The ob_* functions are related to this, ob_flush() would stop/flush the buffer by server : gzip compression, output_buffering in php5.ini does these by client : Browsers like to do less work, so they wait for considerable amount of data to come before showing them to the user

there is a dirty easy option

`yourcommand 1>&2`;

redirecting the stdout to the stderr.

Run process with realtime output in PHP For example if I run 'ping' process it should update my page every time it returns a new Don't work for streaming: shell_exec , exec , passthru; Kind of works: proc_open , popen  12 thoughts on “ Php – Output content to browser in realtime without buffering ” sreejith KB . September 26, 2017 at 4:04 pm. Hello, The code still not working, it’s got stuck because of Suphp, is there any way to stop the buffering by suphp.

I used this solution. It works fine for me.

$commandString = "myexe";

# Uncomment this line if you want to execute the command in background on Windows
# $commandString = "start /b $commandString";

$exec = popen($commandString, "r");

# echo "Async Code Test";

while($output = fgets($exec, 2048))
{
    echo "$output <br>\n";
    ob_flush();
    flush();
}

pclose($exec);

shell_exec. (PHP 4, PHP 5, PHP 7). shell_exec — Execute command via shell and return the complete output as a string  Notifications form a big part of the real time engagement feature of these platforms. Even you are not online, you could still receive these notifications. A PHP notification system could be could be easily built using a mix of vanilla PHP and JavaScript. This system provides real time notification in a PHP powered application.

try this code (tested on Windows machine + wamp server)

        header('Content-Encoding: none;');

        set_time_limit(0);

        $handle = popen("<<< Your Shell Command >>>", "r");

        if (ob_get_level() == 0) 
            ob_start();

        while(!feof($handle)) {

            $buffer = fgets($handle);
            $buffer = trim(htmlspecialchars($buffer));

            echo $buffer . "<br />";
            echo str_pad('', 4096);    

            ob_flush();
            flush();
            sleep(1);
        }

        pclose($handle);
        ob_end_flush();

If you can't see any output or error from system(), shell_exec() etc, you could try this: <?php function my_exec($cmd, $input='') {$proc=proc_open($cmd  First you need to find the location of PHP.ini. Type this command in the shell: php --ini It should output this text: Configuration File (php.ini) Path:

Show output taken from shell_exec and display it in real time instead of after waiting 5-7min When the php file is fired it updates the database, sleeps, etc It is  Realtime updating ColReorder will automatically move columns in realtime as the user moves their mouse. On slower computers with complex tables this can potentially impact performance, so ColReorder has the option to disable this dynamic update and reorder columns only once the reordering is complete - this is done using the colReorder.realtime

I am trying to run a process on a web page that will return its output in realtime. For example if I run 'ping' process it should update my page every time it returns  Note: Relative paths will be resolved as if the current working directory was the one that contains the PHP binary. To use the calling script's working directory, use realpath() or dirname(__FILE__). revno. Revision number to update to, default is SVN_REVISION_HEAD. recurse. Whether or not to recursively update directories.

It uses AJAX to update the tail-windows. Using the php exec command you execute the tail command on the log file, take the output and stick 10 lines each 3 seconds using a #tail -10 logfile (the line could be passed with php shell_exec), The canonical example of real-time is a messaging application. Like when you send a group of friends a text message about getting together for wings on Friday. Then update everyone minute by minute on your progress getting from work to the bar. Thanks, Trevor. Now we’re all trapped in a notification hell that we didn’t sign up for.

Comments
  • Why do you have to flush the buffer?
  • How would I do it with passthru? Also, is there any disadvantage of using @GameBit's code if I don't need to do anything between flushes?
  • You pass the same command as in the solution with popen, but the output from external program goes directly to output... If you know the difference between fopen and readfile, is practically the same thing, only with processes. First allow you read byte by byte and put into a variable, process the data, another simple read and write to output directly, without any action.
  • Got it. Actually is easier to use. Obrigado! :D