如何使用PHP获取目录名(NOT子目录)和文件名


How to get name of directory (NOT subdirectory) and filename with PHP?

我有以下文件夹结构。我只想获取目录(非子目录)和文件的名称。我如何用PHP做到这一点?非常感谢。

Main Directory
 Directory1
  SubDirectory1
   File1
   File2
  SubDirectory2
   File3
   File4
 Directory2
  SubDirectory3
   File5
   File6
  SubDirectory4
   File7
   File8

结果应该是这样的:

Directory: Directory1
File: File1
File: File2
Directory: Directory2
File: File3
File: File4

尝试DirectoryTerator,这里有一个示例

$path = dirname(dirname(__FILE__));
$it = new DirectoryIterator($path);
while($it->valid()){
    /*....*/
    echo $it->getPathname().'">'.$it->getBasename();
    $it->next();//i forgot this just now
}
function display_directory($path){
    $dir_handle = @opendir($path) or die("unable to open $path");
    while ($file = readdir($dir_handle)){
      if($file == "." || $file == "..")
        continue;
      if(is_dir($file))
        echo "directory: $file";
      else
        echo "file: $file";
    }
}