简体   繁体   中英

PHP - display folders and subfolders only at a particular depth

I'm populating a list of directories(the directories are created dynamically)

        $dir = new RecursiveDirectoryIterator('./../data',
            FilesystemIterator::SKIP_DOTS);


        $it  = new RecursiveIteratorIterator($dir,
            RecursiveIteratorIterator::SELF_FIRST);

        $it->setMaxDepth(1);


        foreach ($it as $fileinfo) {
            if ($fileinfo->isDir()) {
                printf("Folder - %s\n", $fileinfo->getFilename());
                $display2 = sprintf('<option id="optionPHP" class="optionPHP" data-icon="glyphicon-time" value="%s">%s</option>' . PHP_EOL, $fileinfo->getFilename(), $fileinfo->getFilename());
                echo $display2;
            }
        }

It displays all folders within 'data' folder. The thing is I want to display directories only at depth../data/Vetri/vetriselvi1

文件夹路径

Essentially I want to populate only folders at that level.. like vetriselvi1, vetriselvi2 ..etc. How do I do that?

So I did a round-about thing to achieve what I wanted to:

foreach ($it as $fileinfo) {
                if ($fileinfo->isDir()) {
                    if (preg_match("/_/",$fileinfo->getFilename()) ||  preg_match("/--Select Option--/",$fileinfo->getFilename())) {
                    $display2 = sprintf('<option id="optionPHP" class="optionPHP" data-icon="glyphicon-time" value="%s">%s</option>' . PHP_EOL, $fileinfo->getFilename(), $fileinfo->getFilename());
                    echo $display2;
                    }
                } 

I tried concatenating the dynamically created folder with a character(for instance here "_"), then did a regex match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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