0

I'm trying to display a list of all folders inside of a directory. But I do not want any folders listed that contain the word "thumbs" in it. Can someone tell me how to do this... Here is the code I am using that doesn't seem to work. It displays all of the folders but it is not blocking the ones with the word "thumbs" in it.

<?php
$dir = new RecursiveDirectoryIterator('puzzle/images/puzzles/',
    FilesystemIterator::SKIP_DOTS);

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

$it->setMaxDepth(1);

foreach ($it as $fileinfo) {
    if ($fileinfo == '*thumbs') {
        // PLACEHOLDER
    } elseif ($fileinfo->isDir()) {
        echo $fileinfo . "<br><br>";
    }
}
?>

The output looks like this....

puzzle/images/puzzles/vehicles

puzzle/images/puzzles/vehicles/thumbs

puzzle/images/puzzles/scenery

puzzle/images/puzzles/scenery/thumbs

puzzle/images/puzzles/movies

puzzle/images/puzzles/movies/thumbs

puzzle/images/puzzles/thumbnails

puzzle/images/puzzles/holidays

puzzle/images/puzzles/holidays/thumbs

puzzle/images/puzzles/holidays/thanksgiving

This is what I want the output to look like except without the following folders...

puzzle/images/puzzles/vehicles/thumbs

puzzle/images/puzzles/scenery/thumbs

puzzle/images/puzzles/movies/thumbs

puzzle/images/puzzles/holidays/thumbs

Or any future folders with the name "thumbs" that I put in there later.

user2284703
  • 367
  • 3
  • 15

1 Answers1

1

$fileinfo == '*thumbs' will only match the exact string '*thumbs', because == does not recognize * as a wildcard.

You want to use something like this: strpos($fileinfo ,'thumbs') !== false

If the filepath needs to end with 'thumbs', you can define an endsWith function*:

function endsWith($haystack, $needle)
{
    return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}

Then call the function like so:

if (endswith($fileinfo, "thumbs")){

}

*from this SO answer

Community
  • 1
  • 1
Jack
  • 2,750
  • 2
  • 27
  • 36
  • That works great... but what if I still want to use a folder such as puzzle/images/puzzles/scenery/thumbs/large ...(where the word thumbs is not at the end of the string)? I guess I forgot to mention that... but I just want to exclude folders paths that END in the word thumbs. – user2284703 Nov 05 '13 at 18:03
  • Jack - U ROCK! Thanks! Everything tested and works perfectly. – user2284703 Nov 05 '13 at 18:11