0

我有以下文件夹结构。我只想获取目录(不是子目录)和文件的名称。我怎么能用 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
4

2 回答 2

3

试试 DirectoryIterator,这是一个例子

$path = dirname(dirname(__FILE__));
$it = new DirectoryIterator($path);
while($it->valid()){
    /*....*/
    echo $it->getPathname().'">'.$it->getBasename();
    $it->next();//i forgot this just now
}
于 2013-07-24T01:10:22.740 回答
0
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";
    }
}
于 2013-07-24T01:40:56.473 回答