即使有错误,如何显示完整的输出?

How to show complete output even though it's error?

我有以下 php 脚本,我在其中尝试在 Windows 上执行命令并打印它的输出。我试过 system()shell_exec()passthru()exec().

$cmd = $_POST['cmd'];
$result = array();
exec($cmd, $result);
foreach ($result as $line ){
 echo $line."<br>";
}

exec($command, $array) 最接近我对执行命令时打印所有内容的期望。我希望打印所有内容,即使它是一个错误。但它只在命令执行成功时打印。

如何实现?

$result只包含命令的标准输出,但错误信息通常写入标准错误。您需要在 运行 命令时将 stderr 重定向到 stdout。

$cmd = "$cmd 2>&1"
exec($cmd, $result);

In the shell, what does " 2>&1 " mean?