php shell_exec with realtime updating

前端 未结 5 742
予麋鹿
予麋鹿 2020-12-17 00:03

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

5条回答
  •  失恋的感觉
    2020-12-17 01:02

    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 . "
    "; echo str_pad('', 4096); ob_flush(); flush(); sleep(1); } pclose($handle); ob_end_flush();

提交回复
热议问题