Currently I am working with directories through php. I am able to list subdirectories for any given directory. However, the results are not 100% what I am looking for. The below code returns subdirectories but in addition it also returns the main directory in the array. How can I only return subdirectories of directory?
$root = '/test';
$iter = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($root, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
);
$paths = array($root);
foreach ($iter as $path => $dir) {
if ( $dir->isDir() ) {
$paths[] = $path;
}
}
Current output
Array ( [0] => /test [1] => /test/asf8 [2] => /test/some2 [3] => /test/something )
Desired output
Array ( [0] => /test/asf8 [1] => /test/some2 [2] => /test/something )
I would change
if ( $dir->isDir() )
to
if ( $dir->isDir() && $dir != $root)
to remove the root directory
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.