Search code examples
phpforeachglobfile-typescandir

PHP - exclude file type when printing img directory


I am writing a simple fishing game in PHP. I have a snippet of code that's printing all of the image files in my /img directory, but it's also outputting .DS_Store. I want to exclude that file, maybe using glob(), but I don't know how. I've googled this for hours with no luck.

$files = scandir('img');
if ($files !== false) {
    foreach($files as $f) {
        if ($f == '..' || $f == '.') continue;
            echo '<li class="fish_pic"><img src="img/'.$f.'" alt="'.$f.'" title="" class="fish"></li>'."\n";
    }
}

How can I exclude .DS_Store?


Solution

  • $files = scandir('img');
    if ($files !== false) {
        foreach($files as $f) {
            if ($f == '..' || $f == '.' || substr($f, -strlen(".DS_Store")) === ".DS_Store") continue;
                echo '<li class="fish_pic"><img src="img/'.$f.'" alt="'.$f.'" title="" class="fish"></li>'."\n";
        }
    }