php - return all unique directories within a hierarchy -
php - return all unique directories within a hierarchy -
i'm using recursivedirectoryiterator
scan files , folders within given root dir. works fine, i'd maintain track of of unique directories in list, i'm not sure recursivedirectoryiterator
way go.
i have directory construction of
-a ->b ->c -one ->two ->three
here code:
<?php function test($dir){ $in_dir = 'none'; $currdir = 'none'; $thisdir = new recursivedirectoryiterator($dir, recursivedirectoryiterator::skip_dots); $files = new recursiveiteratoriterator($thisdir, recursiveiteratoriterator::self_first); foreach($files $object){ //if directory... find out 1 is. if($object->isdir()){ //figure out if have changed directories... $currdir = realpath($object->getpath()); if(strpos($currdir, '.') == false){ $test = strcmp($currdir, $prevdir); if($test){ echo "current dir changing: ", $currdir, "\n"; $prevdir = $currdir; } } } } } test('fold'); ?>
what following:
current dir changing: /users/<usr>/desktop/test/fold
current dir changing: /users/<usr>/desktop/test/fold/a
current dir changing: /users/<usr>/desktop/test/fold/a/b
current dir changing: /users/<usr>/desktop/test/fold
current dir changing: /users/<usr>/desktop/test/fold/one
current dir changing: /users/<usr>/desktop/test/fold/one/two
...but want unique directories.
it's perhaps method of object comparing in loop returns duplicates, iterator moves , downwards directory tree parses through folders.
the next worked me. utilize array_unique()
confirm no dupes redundancy.
$dirarray = []; // array store dirs $path = realpath('/some/folder/location'); $objects = new recursiveiteratoriterator( new recursivedirectoryiterator($path), recursiveiteratoriterator::self_first ); // loop through objects , store names in dirarray[] foreach($objects $name => $object){ if ($object->isdir()) { $dirarray[] = $name; } } // create sure there no dupes $result = array_unique($dirarray); // print array out print_r($result);
php recursion iterator directory
Comments
Post a Comment