简体   繁体   English

仅获得主文件夹而没有子文件夹

[英]Only get the main folders and no subfolders

I want to get the main folder with no subfolders with the following code: 我想使用以下代码获取没有子文件夹的主文件夹:

$dir = new RecursiveDirectoryIterator($admin['data_folder_main'], FilesystemIterator::SKIP_DOTS);
$it = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST);
$it->setMaxDepth(1);

foreach($it AS $fileinfo) {
    if($fileinfo->isDir() AND !$it->isDot()) {
        echo $it->getSubPathName().'<br>';
    }
}

As it is right now, this code prints 现在,此代码将打印出来

main-folder
main-folder/subfolder
main-folder/subfolder

Does RecursiveDirectoryIterator or RecursiveIteratorIterator have a function that says that "this is the main folder"? RecursiveDirectoryIteratorRecursiveIteratorIterator是否具有一个说“这是主文件夹”的函数? Or do I have to use some workaround code? 还是我必须使用一些解决方法代码? If yes, how will that code look like? 如果是,该代码将如何显示?

Nevermind! 没关系! I fixed it like this: 我这样修复:

$dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($admin['data_folder_main']), FilesystemIterator::SKIP_DOTS, RecursiveIteratorIterator::SELF_FIRST);

foreach($dir as $name => $object) {
    if($dir->isDir() AND !$dir->isDot()) {
        echo basename($name);
    }
}

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

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