Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 488
EduAlm Avatar asked Dec 03 '11 20:12

EduAlm


2 Answers

if( ($fp = popen("your command", "r")) ) {
    while( !feof($fp) ){
        echo fread($fp, 1024);
        flush(); // you have to flush buffer
    }
    fclose($fp);
}
like image 161
RolandasR Avatar answered Oct 20 '22 11:10

RolandasR


there is a dirty easy option

`yourcommand 1>&2`;

redirecting the stdout to the stderr.

like image 3
useless Avatar answered Oct 20 '22 11:10

useless