John
John

Reputation: 227

php foreach glob multiple extensions

There has got to be a better way to write this:

<?php $imagecounter = "no";
    foreach (glob("images/*.jpg") as $image)    {
    $imagecounter = "yes";
    }
    foreach (glob("images/*.png") as $image)    {
    $imagecounter = "yes";
    }
    foreach (glob("images/*.gif") as $image)    {
    $imagecounter = "yes";
    }
    if ($imagecounter == "yes"){Create gallery}?>

That folder might have zip or pdf files too that should not create a gallery

Upvotes: 2

Views: 2973

Answers (2)

pratik patell
pratik patell

Reputation: 20

I think you want to remove foreach looping and check that in images folder any extension from "jpg,png,gif". so you can use ternary operator. Here is sample code.

$imagecounter = glob("img/*.jpg")?'yes':$imagecounter;
$imagecounter = glob("img/*.png")?'yes':$imagecounter;
$imagecounter = glob("img/*.gif")?'yes':$imagecounter;

if ($imagecounter == "yes"){Create gallery}

?> If i assume wrong, let me know mate.

Upvotes: -2

Hanky Panky
Hanky Panky

Reputation: 46900

if(glob("images/*.{jpg,png,gif}", GLOB_BRACE))
{
  //create gallery
}

And that's about it :)

Upvotes: 10

Related Questions