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 联系我们: