当前位置:网站首页>PHP script calls command to get real-time output

PHP script calls command to get real-time output

2022-06-24 21:12:00 The smell of tobacco

When writing scripts , It is often necessary to call other commands . While invoking some time-consuming commands , We need to be able to control the script progress in real time .

Generally speaking , The progress of the script is usually obtained through the output of the script .

If it's a bash Script , Then call directly command A You can hand over the power of execution , then command A The output of can be displayed in real time .

If it is perl Script , adopt `` Symbols can also call command A, But the result will be command A You can only get it at the end of the whole , At this point through system command A You can hand over the power of execution to command A, Again , command A The output of can also be displayed in real time .

If it is PHP Script , adopt `` Symbol call , and perl The script is the same , Only in command A Only when it is over can we get back . that PHP How to get the script in real time command A The output of ? You can listen to the process output :

function real_cmd($command){
    
    //  Execute the command and open the output pointer of the command 
    $handle = popen($command, 'r');
    while (!feof($handle)) {
    
        //  Loop through command output 
        $data = fgets($handle);
        //  Refresh output buffer 
        ob_flush();
        flush();
        echo $data;
    }
    //  End of command execution ,  Close the pointer 
    pclose($handle);
}
原网站

版权声明
本文为[The smell of tobacco]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211318416041.html