user1383147
user1383147

Reputation: 1083

PHP file listing multiple file extensions

Here is my current code:

$files = glob("*.jpg");

This works fine. However, I am wanting to list other image types, such as .png, gif etc.

Can I please have some help to modify this above code to get it working. I have tried the following with no success:

$files = glob("*.jpg","*.png","*.gif");

$files = glob("*.jpg,*.png,*.gif);

And other variations...

Upvotes: 69

Views: 35421

Answers (5)

Dexter
Dexter

Reputation: 9324

05 2021

This is just an expansion of @Jeroen answer.

Somethings to keep in mind

'flag' is Important

Since you are using curly brackets, keep in mind GLOB_BRACE is required. Without the flag, you will get a empty array of items

$files = glob("*.{jpg,png,gif}", GLOB_BRACE);

Sorting

This will also help you to sort the files in the way you have written.
The sorting below is based on the order of extensions inside the curly bracket.

$files = glob("*.{jpg,png,gif}", GLOB_BRACE);
xx.jpg
xx.jpg
xx.png
xx.gif
xx.gif

$files = glob("*.{gif,jpg,png}", GLOB_BRACE);
xx.gif
xx.gif
xx.jpg
xx.jpg
xx.png

+ Bonus

If you have to list out all the files but without a folder, you can use this

$files = glob("*.{*}", GLOB_BRACE);

Upvotes: 6

NoFunction
NoFunction

Reputation: 31

I found a much easier solution than using GLOB_BRACE and it is case insensitive:

$files = array_filter(glob('path/*.*'), function ($filename) { return preg_match('/\.(jpe?g|png|gif)$/i', $filename); });
sort($files);

Or you could simply do it like this:

$files = preg_grep('/\.(jpe?g|png|gif)$/i', glob('path/*.*'));
sort($files);

Just my two cents, hope it helps anyone who ends up here.

Upvotes: 3

Jens Törnell
Jens Törnell

Reputation: 24768

I just needed this for my own project. I made a converter from array to string.

function whitelistToBrace($whitelist) {
  $str = "{";
  $whitelist = !empty($whitelist) ? $whitelist : ['*'];

  foreach($whitelist as $extension) {
    $str .= '*.' . strtolower($extension) . ',';
  };

  $str = substr($str, 0, -1) . '}';

  return $str;
}

Usage

$whitelist = [
  'png',
  'jpg'
];

// glob('my/path/*.{*.png,*.jpg}', GLOB_BRACE);
$glob = glob('my/path/' . whitelistToBrace($whitelist), GLOB_BRACE);
print_r($glob);

Upvotes: 0

Marco Santana
Marco Santana

Reputation: 121

My two cents:

$availableImageFormats = [
"png",
"jpg",
"jpeg",
"gif"];
$searchDir = /*yourDir*/;
$imageExtensions = "{";
foreach ($availableImageFormats as $extension) {
    $extensionChars = str_split($extension);
    $rgxPartial = null;
    foreach ($extensionChars as $char) {
        $rgxPartial .= "[".strtoupper($char).strtolower($char)."]";
    }
    $rgxPartial .= ",";
    $imageExtensions .= $rgxPartial;
};
$imageExtensions .= "}";
glob($searchDir."/*.".$imageExtensions, GLOB_BRACE)

With this you can create an array of all the extensions you are looking for without worrying of improper case use. Hope it helps

Upvotes: 1

Jeroen
Jeroen

Reputation: 13257

$files = glob("*.{jpg,png,gif}", GLOB_BRACE);

Upvotes: 156

Related Questions