php执行neverending binary / script print输出

[英]php execute neverending binary/script print output live

I'm running into a problem where the php script never terminates due to its work, so it never gets a chance to pass data back to ajax to display back to the user in html format.


To be more clear, what I'm trying to do is have a user click on a button on an html page, the button executes a javascript function which uses ajax to execute the php script. The php script executes a never ending command (ie: ping localhost) using popen and then I echo the output from popen. The problem is that because the php script never finishes, the echos are never reported back unless I kill the ping process, which causes the php script to end and the data echoed returned back to the javascript function to display in html.

更清楚的是,我要做的是让用户点击html页面上的按钮,该按钮执行一个javascript函数,该函数使用ajax来执行php脚本。 php脚本使用popen执行一个永不停止的命令(即:ping localhost),然后我回显popen的输出。问题是因为php脚本永远不会完成,所以回不会报告回声,除非我终止ping进程,这导致php脚本结束并且数据回显返回到javascript函数以在html中显示。

I want to be able to see the live output when the php script is running. If I click on that button in the html website, I want the button to call the javascript function, then execute the php script and I want to see immediately every echo from the php script as it is running live. Is that possible?


Here is my code so far:



<!DOCTYPE html>
    <meta charset="utf-8" />
    <script type="text/javascript" src=""></script>
    <script src=""></script>

        function execfunc() {
            // Clear any previous output.

                type: "GET",
                url: "phpscript.php",
                data: "",
                success: function(msg){
            }); // Ajax Call

        function stopReadout() {
            // to do

        function clearOutput() {
    <h3>Executing Linux Command</h3>
    <p>Push the button to execute the command "ping localhost":</p>
    <button type="button" onclick="execfunc();">Execute</button>
    <button type="button" onclick="stopReadout();">Stop</button>
    <button type="button" onclick="clearOutput();">Clear</button>
    <p id="label">Terminal output:</p>
    <div id="divOutput" style="width:800px;height:512px;border:1px solid black;overflow:scroll"></div>


    while (@ ob_end_flush()); // end all output buffers if any
    $cmd = 'ping localhost';
    $proc = popen($cmd, 'r');

    echo '<pre>';
    while (!feof($proc))
        echo fread($proc, 4096);
    echo '</pre>';

If you run the phpscript.php by itself, it works fine and echos back to the browser window. But if I execute the script from the index.html javascript using ajax, I don't get any live output back because the php script never finishes.

如果您自己运行phpscript.php,它可以正常工作并回显到浏览器窗口。但是如果我使用ajax从index.html javascript执行脚本,我不会得到任何实时输出,因为php脚本永远不会完成。

I even tried having make a fifo pipe and dump the ping output into the fifo pipe, then I had another php script that echoed back 'cat fifofile', but the same behavior happens.

我甚至尝试让phpscript.ph制作一个fifo管道并将ping输出转储到fifo管道中,然后我有另一个PHP脚本回显'cat fifofile',但是同样的行为发生了。

Any suggestions or help would truly be appreciated it! D

任何建议或帮助将真正受到赞赏! d

1 个解决方案



You need to use web sockets for that.




  © 2014-2022 联系我们: