我正在尝试在 PHP 中执行系统命令并捕获那些输出涵盖多行的命令的输出。我正在使用 exec(),但它似乎只为在一行上生成输出的命令返回一个值。
例如,如果我在命令行运行 date,我会得到:
Wed May 15 15:07:32 EST 2013
如预期的那样,如果我从 PHP 作为 exec 使用此命令运行此命令...
exec("date", $exec_results);
...然后 $exec_results 的值变成...
Array ( [0] => Wed May 15 15:07:32 EST 2013 )
但是,当我从命令行运行时,我得到了这个...
real 0m0.000s
user 0m0.000s
sys 0m0.000s
...但是当我用这个从 PHP 执行时...
exec("time", $exec_results);
... $exec_results 的值为空:
Array( )
我实际上不需要在我的应用程序中运行日期或时间,但这些只是命令行上的单行与多行输出似乎如何改变返回 PHP 的内容的示例。
文档说:
If the output argument is present, then the specified array will be filled with every line of output from the command.
那么,为什么 $exec_results 数组没有填充在命令行中运行时间命令时看到的所有行?
注意 - 我已经以 apache 用户身份运行命令行条目以排除特权。
最佳答案
这应该适合你
ob_start();
passthru("ls -la");
$dat = ob_get_clean();
关于php - 如何捕获用 PHP exec 发出的命令的多行输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16557333/