php获取所有子目录和所有文件的列表

时间:2011-12-11 17:05:25

标签: php linux unix ls

获取php中给定目录下所有子目录列表和所有文件的另一个列表的最佳方法是什么?只要我可以从php(例如c / java / python / ...程序)使用它,我就可以使用非纯PHP代码。比纯recursion更快的东西,某种语言内置的东西(因为这些东西往往很快。)

6 个答案:

答案 0 :(得分:4)

查看RecursiveDirectoryIterator

foreach (new RecursiveDirectoryIterator('yourDir') as $file) {
    // you don't want the . and .. dirs, do you?
    if ($file->isDot()) {
        continue;
    }

    if ($file->isDir()) {
        // dir
    } else {
        // file
    }
}

答案 1 :(得分:2)

如果你不喜欢OOing的东西,你可以通过find的结果运行一个opendir()循环。

if (exec('find /startdir -type d -print', $outputarray)) {
  foreach ($outputarray as $onepath) {
    // do stuff in $onepath
  }
}

你确实指定了“非纯PHP”作为选项,对吗? : - )

答案 2 :(得分:1)

摘自glob()上的php.net文档:

$path[] = 'starting_place/*';

while(count($path) != 0) {
  $v = array_shift($path);

  foreach(glob($v) as $item) {
    if(is_dir($item))
      $path[] = $item . '/*';
    else if (is_file($item)) {
      //do something
    }
  }
}

答案 3 :(得分:1)

class Dir_helper{
    public function __construct(){

    }
    public function getWebdirAsArray($rootPath){
        $l1 = scandir($rootPath);
        foreach ($this->getFileList($rootPath) as $r1){
        if ($r1['type'] == 'dir'){
            if (preg_match("/\./", $r1['name'])){
            $toplevel[] =  $r1['name'];
            } else {
            if (preg_match("/\d/",$r1['name'])){
                $seclevel[] = $this->getFileList($r1['name']);
            }
            }
        }
        }
        foreach ($seclevel as $sl){
        foreach ($sl as $cur){
            $sub[] = $cur['name'];
        }
        }
        return $result = array_merge((array)$toplevel, (array)$sub);
    }

    public function getFileList($dir){
        $retval = array();
        if(substr($dir, -1) != "/") $dir .= "/";
        $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
        while(false !== ($entry = $d->read())) {
            if($entry[0] == ".") continue;
            if(is_dir("$dir$entry")) {
                $retval[] = array(
                "name" => "$dir$entry/",
                "type" => filetype("$dir$entry"),
                "size" => 0,
                "lastmod" => filemtime("$dir$entry")
                );
            } elseif(is_readable("$dir$entry")) {
                $retval[] = array(
                "name" => "$dir$entry",
                "type" => mime_content_type("$dir$entry"),
                "size" => filesize("$dir$entry"),
                "lastmod" => filemtime("$dir$entry")
                );
            }
        }
        $d->close();
        return $retval;
    }

}

答案 4 :(得分:0)

使用php的内置RecursiveDirectoryIterator

修改

类似的东西:

$dirs  = array();
$files = array();

$dir = __DIR__ . '/foo';

$iterator = new RecursiveDirectoryIterator(new DirectoryIterator($dir));

foreach ($iterator as $dirElement) {
    if ($dirElement->isDir()) {
        $dirs[] $dirElement->getPathname();
    }
    if ($dirElement->isFile()) {
        $files[] = $dirElement->getPathname();
    }
}

答案 5 :(得分:0)

由于您不希望递归,所以我只写了一些额外的内容

// $dirs = [];
// Get All Files & Folders in $dir
$files = glob("$dir/*");
for ($i=0; $i < count($files); $i++) { 
    if (is_dir($files[$i])) {
        $files = array_merge($files, glob("$files[$i]/*"));
    //  $dirs[] = $files[$i]; // This can add the folder to a dir array
    }
}
// Remove folders from the list if you like
foreach ($files as $key => $file) {
    if (is_dir($file)) {
        unset($files[$key]);
    }
}
// Clean up the key numbers if you removed files or folders
$files = array_values($files);