任何人都可以幫助,如何從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替換
感謝
只是一個方面說明:你的HTML形式的語法是錯誤的。 – reporter
您是否有錯誤或警告訊息?什麼是不工作,請給更多的細節。 另外,你的代碼開頭沒有'<?php'。 –
@reporter和Benoit Zu,我已更正代碼 –