duanchi5078 2012-02-15 21:46
浏览 605
已采纳

在php中运行shell命令,实时输出

I am uploading a file via php move_uploaded_file function and then the file is then taken as a parameter of a java program. while the java program is executing, it outputs the progress in the terminal. The output of the java program is input of a perl script which does the same. I am executing all of them in my php web site as shell_exec functions and printing them on screen

    echo "Running java ..... ";
    $output=shell_exec("java -jar abc.jar ".( $_FILES['uploaded']['name']));
    echo "<pre>$output</pre>";
    echo "running perl .....";
    $output=shell_exec("perl abc.pl outputfromjava.txt");
    echo "<pre>$output</pre>";

The shell commands are running fine but the output from java and perl isn't displayed on my web site. I tried refreshing the page in my code after java and perl is executed but it then refreshes the upload too.

The output is now

Running java ....
Running Perl ....

How should I write my php so that the output is

Running Java...
Progress1 //from java output
progress2 //from java output
Running Perl....
Progress1 //from perl output
progress2 //from perl output

Thanks

  • 写回答

3条回答 默认 最新

  • drzil26260 2012-02-15 22:04
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记