【发布时间】:2017-08-24 14:00:36
【问题描述】:
任何人都可以帮助,如何使用 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 替换它不起作用
谢谢
【问题讨论】:
-
附注:表单的 HTML 语法错误。
-
您有错误或警告信息吗?什么不起作用,请提供更多详细信息。此外,您的代码开头没有
<?php。 -
@reporter 和 Benoit Zu,我已经更正了代码
-
html 缺少一个开始 标记。
-
已添加但无法正常工作