Reputation: 1806
I'm trying to return all unique instances paths from a specified directory, recursively.
I'm using RecursiveDirectoryIterator
. I also would like to omit any instances of paths that contain '.' in them, which is where I'm having trouble.
Here's what I've got going as a test:
<?php
function test($dir){
$dirArray = []; // the array to store dirs
$path = realpath($dir);
$dirs = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
$objects = new RecursiveIteratorIterator($dirs, RecursiveIteratorIterator::SELF_FIRST);
// loop through all objects and store names in dirArray[]
foreach($objects as $name => $object){
if($object->isDir() && strpos($object->getBasename(), '.') !== true) {
$dirArray[] = $name;
echo "test: " . $object->getBasename() . "\n";
}
}
print_r($dirArray);
}
test('/some/dir');
?>
This code nearly does what I need. It returns all unique dirs, but includes those with a '.' in the path name.
Upvotes: 0
Views: 147
Reputation: 41885
Just add another checker inside, and try to use ->getPathname()
instead:
if($object->isDir() && strpos($object->getPathname(), '.') === false) {
// do some stuff
}
This just basically means, if this is a directory AND if pathname does not contain that .
Upvotes: 1