2017-08-24 7 views
3

誰でも助けることができます、PHPを使用してLinuxでコマンド出力の出力からリアルタイム出力を読む方法。私は、ボタンを提出し、私は何を提出していない押したときに来ている取得していますコマンドライン出力を読む

<?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ドキュメントが閉じます。


pingの出力が<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>"; 

接続が行われたことをecks(あなたが本当にここelseを追加する必要があります):今すぐ

if (is_resource($process)) { 

何?

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

これは間違いありません。これは永遠に回転して回転します。一方、これのどれも実行されません取得します。

?> 

</body> 
</html> 

だから、ブラウザ近い体を見たことがありません。だから、何を尋ねますか?まあ、ブラウザは、すべてのタグを閉じるまでコンテンツを表示する義務はありません。

「ただし、時にはうまくいきます!」あなたは叫ぶ。確かに、時にはそれは動作します。しかし、今回はローディングがちょうどが遅れているように見えます()。ブラウザは、決して終わらない無限ループを持っていることを知らないので、出力を閉じるのを待っているだけです。

In general

Web作成者は用心 - 整形HTMLを書く - あなたはWebkitのエラー許容コードの例として表示する場合を除きます。バック作業を行うスクリプトを呼び出し、

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

またはJavaScript負荷を持っている:あなたはこの作品を見たい場合は

は、その後、無限ループをしません。

+0

まだ動作していません。投稿をクリックした後に通知が表示されます。 –

+0

'ls'が動作するのは、すべてのファイルをリストした後に出力が終了するためです。 'ping'は決して終了しないので、あなたは無限のループを持っています。ブラウザは決して身体を閉じないと見ています。コマンドを 'ping -c 4 127.0.0.1'に変更すると、出力が表示されます。 – bishop

+0

いいえ運がありません-c 4オプションを使用しました –

関連する問題