2017-08-24 177 views
3

任何人都可以幫助,如何從Linux命令終端輸出使用PHP讀取實時輸出。閱讀命令行輸出

下面是代碼,我試過,但沒有工作:

<?php 
if (isset($_POST['pyinp'])){ 
$cmd = "ping 127.0.0.1"; 
$descriptorspec = array(
    0 => array("pipe", "r"), // stdin is a pipe that the child will read from 
    1 => array("pipe", "w"), // stdout is a pipe that the child will write to 
    2 => array("pipe", "w") // stderr is a pipe that the child will write to 
); 
flush(); 
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array()); 
echo "<pre>"; 
if (is_resource($process)) { 
    while ($s = fgets($pipes[1])) { 
     print $s; 
     flush(); 
    } 
} 
echo "</pre>"; 
} 
?> 
<!DOCTYPE html> 
<html> 
<body> 
<form action="#" method="POST"> 
<input name = "pyinp" type="submit"> 
</form> 
</body> 
</html> 

我正在提交按鈕,當我按下提交什麼來了。

我是新來的HTML和PHP。我只是在網上編碼。

下面代碼工作:

<!DOCTYPE html> 
<html> 
<body> 
<form action="#" method="POST"> 
<input name = "pyinp" type="submit"> 
</form> 
<?php 
if (isset($_POST['pyinp'])){ 
$cmd = "ping 127.0.0.1"; 
$descriptorspec = array(
    0 => array("pipe", "r"), // stdin is a pipe that the child will read from 
    1 => array("pipe", "w"), // stdout is a pipe that the child will write to 
    2 => array("pipe", "w") // stderr is a pipe that the child will write to 
); 
echo "<pre>"; 
if(($fp = popen("ls -l", "r"))) { 
    while(!feof($fp)){ 
     echo fread($fp, 1024); 
     flush(); // you have to flush buffer 
    } 
    fclose($fp); 
} 
} 
?> 
</body> 
</html> 

但是,如果我用ping 127.0.0.1替換

感謝

+1

只是一個方面說明:你的HTML形式的語法是錯誤的。 – reporter

+2

您是否有錯誤或警告訊息?什麼是不工作,請給更多的細節。 另外,你的代碼開頭沒有'<?php'。 –

+0

@reporter和Benoit Zu,我已更正代碼 –

回答

0

TL它不工作; DR:你之前有一個無限循環HTML文檔關閉。


讓我重寫代碼,使平輸出應該進入<body>...</body>容器:

<!DOCTYPE html> 
<html> 
<body> 
<form action="#" method="POST"> 
<input name = "pyinp" type="submit"> 
</form> 
<?php 
if (isset($_POST['pyinp'])){ 
$cmd = "ping 127.0.0.1"; 
$descriptorspec = array(
    0 => array("pipe", "r"), // stdin is a pipe that the child will read from 
    1 => array("pipe", "w"), // stdout is a pipe that the child will write to 
    2 => array("pipe", "w") // stderr is a pipe that the child will write to 
); 
flush(); 
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array()); 
echo "<pre>"; 
if (is_resource($process)) { 
    while ($s = fgets($pipes[1])) { 
     print $s; 
     flush(); 
    } 
} 
echo "</pre>"; 
} 
?> 

</body> 
</html> 

現在,讓我們做一個思想實驗。第一次你GET的頁面,你看到一個按鈕的形式。一切都很好。你點擊按鈕。表格會自動回到POST。那麼會發生什麼?

首先,靜態HTML是輸出:

<!DOCTYPE html> 
<html> 
<body> 
<form action="#" method="POST"> 
<input name = "pyinp" type="submit"> 
</form> 

然後PHP引擎激活:

<?php 

然後PHP看到按鈕被按下,並做了一堆東西設置配管(所有這一切都是正確的,順便說一句,你可以檢查命令行):

$cmd = "ping 127.0.0.1"; 
$descriptorspec = array(
    0 => array("pipe", "r"), // stdin is a pipe that the child will read from 
    1 => array("pipe", "w"), // stdout is a pipe that the child will write to 
    2 => array("pipe", "w") // stderr is a pipe that the child will write to 
); 
flush(); 
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array()); 
echo "<pre>"; 

然後CH ecks該連接時(你真的應該添加一個else這裏):

if (is_resource($process)) { 

現在怎麼辦?

while ($s = fgets($pipes[1])) { 
    print $s; 
    flush(); 
} 

沒錯:while循環永遠不會結束!這種旋轉和旋轉永遠。同時,這一切都不被執行:

?> 

</body> 
</html> 

因此,瀏覽器永遠看不到人體接近。那麼你問什麼?那麼,在關閉所有標籤之前,瀏覽器沒有義務顯示內容。

「但是,有時它確實有效!」你喊。當然,有時它是有效的。但是這一次,看起來加載僅僅是停頓,而不僅僅是錯誤的HTML。瀏覽器不知道你有一個無限循環永遠不會結束,所以它只是在等待你關閉你的輸出。

In general

Web作者當心 - 除非你想出現在Webkit容錯的代碼的例子 - 寫的很好的HTML。

如果你想看到這項工作,那就不要做一個無限循環:

$i = 0; 
while ($s = fgets($pipes[1])) { 
    print $s; 
    flush(); 
    if (4 < $i++) { break; } 
} 

或者具有的JavaScript load,回撥到一個腳本,做的工作。

+0

仍然無法正常工作。點擊提交後注意到正在顯示在後添加更多的信息 –

+0

'ls'工作的原因是因爲它的輸出在列出所有文件後結束。 'ping'永遠不會終止,因此你有一個無限循環,瀏覽器永遠不會看到身體關閉,並且它堅持等待。將你的命令改成'ping -c 4 127.0.0.1',你會看到輸出。 – bishop

+0

運氣不好我已經用-c 4選項 –