0

我有以下工作代码,它读取文件包含并放入数组,但现在我想运行命令并将其输出放在数组中,例如ls命令

<?php
$path = "file.txt";
$file = fopen($path, 'r');
$data = fread($file, filesize($path));
 fclose($file);
    $lines =  explode(" ",$data);
                echo "<p><h1>$lines[0]</h1></p>";
?>

我如何读取命令输出并放入数组中?

4

2 回答 2

1

您可以使用shell_exec

<pre><?php
$output = shell_exec('ls');
print_r($output);
于 2013-06-04T02:52:22.010 回答
0

我的首选方法是使用popen。将结果放入数组中将是微不足道的

 $fp = popen ("ls -l", "r");
 $array = array();
 while ($rec = fgets($fp)){
     $array[] = trim($rec);
 }
 // do something cool with array
于 2013-06-04T03:19:10.250 回答