我的php代码exec('ls -l')不会包含所有文件

问题描述 投票:0回答:3

我有一个/Users/dele/Desktop/TestIOS/testPhp01/cms/test04.php文件:

<?php
/**
 * Created by PhpStorm.
 * User: dele
 * Date: 2019/9/26
 * Time: 11:56 AM
 */

exec("ls -l");

[当我运行此文件时,仅显示一个文件输出,

/usr/local/Cellar/[email protected]/7.0.30_1/bin/php /Users/dele/Desktop/TestIOS/testPhp01/cms/test04.php
string(54) "-rw-r--r--  1 dele  staff   133 Oct  8 12:03 test04.php"

Process finished with exit code 0

但是在我的目录/Users/dele/Desktop/TestIOS/testPhp01/cms/中,有那些文件:

dele-MBP:cms ldl$ ls -l
total 48
-rw-r--r--  1 ldl  staff     0 Aug 20 16:49 1
-rw-r--r--  1 ldl  staff    42 Aug 20 16:49 2.js
-rw-r--r--  1 ldl  staff   165 Feb 18  2019 index.php
-rw-r--r--  1 ldl  staff   551 Aug 30 17:36 test01.php
-rw-r--r--  1 ldl  staff  1308 Sep  8 00:38 test02.php
-rw-r--r--  1 ldl  staff  1444 Sep 10 23:09 test03.php
-rw-r--r--  1 ldl  staff   107 Oct  8 11:56 test04.php
php exec ls
3个回答
1
投票

尝试使用输出参数:

exec("ls -l", $r);
var_dump($r);

0
投票

根据手动exec条目,它明确表示:

返回值命令结果的最后一行。

因此它仅显示最后一行。

因此只需使用passthru('ls -l')即可获取全部数据。

Reference


0
投票
<?php

exec("ls -l", $items, $return_status);

foreach ($items as $key => $item) {
  printf("%s\n", $item);
}

Exec documentation

© www.soinside.com 2019 - 2024. All rights reserved.