使用glob返回给定目录中的文件夹列表(没有路径)

Sco*_*t B 2 php

下面的函数将给定目录中的所有文件夹返回到多个级别.

我只需要一个级别的深度,只是目标目录中的文件夹,没有子文件夹.

该函数还返回文件夹的完整路径,我只想要文件夹名称.我确定我错过了一些简单的事情.

如何修改函数以仅返回给定目录的文件夹名称?(不是每个文件夹的完整路径)

$ myArray = get_dirs('../ wp-content/themes/mytheme/images');

<?php
  function get_dirs( $path = '.' ){
    return glob( 
      '{' . 
        $path . '/*,'    . # Current Dir
        $path . '/*/*,'  . # One Level Down
        $path . '/*/*/*' . # Two Levels Down, etc.
      '}', GLOB_BRACE + GLOB_ONLYDIR );
  }
?>
Run Code Online (Sandbox Code Playgroud)

顺便说一句,感谢Doug对原始功能的帮助!

Jor*_*ore 5

而不是使用glob(),我建议使用DirectoryIterator该类.

function get_dirs($path = '.') {
    $dirs = array();

    foreach (new DirectoryIterator($path) as $file) {
        if ($file->isDir() && !$file->isDot()) {
            $dirs[] = $file->getFilename();
        }
    }

    return $dirs;
}
Run Code Online (Sandbox Code Playgroud)