简体   繁体   English

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

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

I have following folder structure. 我有以下文件夹结构。 I want to get names of only directories (NOT subdirectories) and files . 我只想获取目录 (不是子目录)和文件的名称 How can I do that with PHP? 如何使用PHP做到这一点? Thank you. 谢谢。

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

Result should be something like that: 结果应该是这样的:

Directory: Directory1
File: File1
File: File2

Directory: Directory2
File: File3
File: File4

Try DirectoryIterator,here is an example 试试DirectoryIterator,这是一个例子

$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";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM