在php网页中逐行打印shell命令的输出行

在php网页中逐行打印shell命令的输出行,php,shell,Php,Shell,我试图在文本区域中逐步显示命令的输出行。 这是我已经有的代码。部分工作正常: if (!empty($_POST["ACTION"])) { $fds = array( 1 => array("pipe", "w"), 2 => array("pipe", "w"), ); $cmd = "command -option --debug 2>&1"; $res = proc_open($cmd, $fds,

我试图在文本区域中逐步显示命令的输出行。 这是我已经有的代码。部分工作正常:

if (!empty($_POST["ACTION"])) {
    $fds = array(
        1 => array("pipe", "w"),
        2 => array("pipe", "w"),
    );
    $cmd = "command -option --debug 2>&1";
    $res = proc_open($cmd, $fds, $pipes);
    if (!is_resource($res))
        $res = false;
}
echo '<form method="post" action="?itemtype=item">';

if (is_resource($res)) {
    ini_set("max_execution_time", 0);
    ignore_user_abort(true);
    set_time_limit(0);

echo '<textarea readonly="readonly" id="action_done" style="display: block; width: 99%; height: 280px">';
do {
    $read = $pipes;
    $exc = $pipes;
    $write = array();
    $nb = stream_select($read, $write, $exc, static::TIMEOUT, 0);
    // Error
    if ($nb === FALSE) {
         echo "UNKNOWN ERROR\n";
         break;
    }

    // Timeout
    if ($nb === 0) {
        echo "ERROR: command timed out!\n";
        break;
    }

    if (count($exc)) {
        echo "UNKNOWN ERROR\n";
        break;
    }
    foreach ($read as $stream){
         echo htmlspecialchars(fread($stream, 8192), ENT_HTML5 | ENT_QUOTES, "utf-8");
         flush();
         ob_flush();
    }
    if (feof($pipes[1])){
         break;
    }
} while(1);
$info = proc_get_status($res);
if ($info === false) {
     echo "ERROR: could not determine process status\n";
} else {
   if ($info["signaled"])
       echo "Command terminated by signal ${info['termsig']}\n";
   if ($info["stopped"])
       echo "Command stopped by signal ${info['stopsig']}\n";
       echo "Command exited with return code ${info['exitcode']}\n";
}

proc_close($res);
echo '</textarea>';
}
if(!empty($\u POST[“ACTION”])){
$fds=数组(
1=>数组(“管道”,“w”),
2=>阵列(“管道”,“w”),
);
$cmd=“command-option--debug 2>&1”;
$res=proc_open($cmd、$fds、$pipes);
如果(!是_资源($res))
$res=假;
}
回声';
如果(是资源($res)){
ini_集(“最大执行时间”,0);
忽略用户中止(true);
设置时间限制(0);
回声';
做{
$read=$pipes;
$exc=$pipes;
$write=array();
$nb=stream\u select($read,$write,$exc,static::TIMEOUT,0);
//错误
如果($nb==FALSE){
回显“未知错误\n”;
打破
}
//超时
如果($nb==0){
echo“错误:命令超时!\n”;
打破
}
如果(计算($exc)){
回显“未知错误\n”;
打破
}
foreach($读作$stream){
echo htmlspecialchars(fread($stream,8192),ENT|u HTML5 | ENT|u引号,“utf-8”);
冲洗();
ob_flush();
}
if(feof($pipes[1])){
打破
}
}而(1),;
$info=proc\u get\u status($res);
如果($info==false){
echo“错误:无法确定进程状态\n”;
}否则{
如果($info[“信号”])
echo“由信号${info['termsig']}终止的命令\n”;
如果($info[“已停止”])
echo“由信号${info['stopsig']}停止的命令\n”;
echo“命令退出,返回代码为${info['exitcode']}\n”;
}
过程结束($res);
回声';
}
我之所以说它部分工作是因为它在while循环中的某个点停止。我不知道为什么。 更准确地说,我没有输出的结尾(它应该告诉我一切都很好),但它没有。 只有当命令检测到错误时,它才能正常工作,循环才会正常工作。 因此,如果有人知道这段代码中的错误,并告诉我要更改什么 或 知道如何以不同方式执行(即在文本区域中逐步显示命令的输出),并告诉我如何。。。 这将是巨大的。
谢谢

启用错误报告检查是否存在任何错误,或者是否使用E_ALL&~E_DEPRECATED&~E_STRICT打开了notError报告。我没有任何错误:(好的,我最后有一个错误:它说管道没有定义,ob_flush没有缓冲区来刷新…但我不明白,“管道”已定义…您遇到了什么错误?打开错误报告检查是否存在任何错误,或使用E_ALL&~E_DEPRECATED&~E_STRICT打开错误报告。我没有任何错误:(好的,我终于有一个错误:它说管道未定义,ob_flush没有要刷新的缓冲区…但我不明白,“管道”定义了…您得到了什么错误?