PHP file listing multiple file extensions

Php

Php Problem Overview


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...

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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 required. Without the flag you will get a empty array if 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 folder, you can use this

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

Solution 3 - Php

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

Solution 4 - Php

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);

Solution 5 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1383147View Question on Stackoverflow
Solution 1 - PhpJeroenView Answer on Stackoverflow
Solution 2 - PhpDexterView Answer on Stackoverflow
Solution 3 - PhpMarco SantanaView Answer on Stackoverflow
Solution 4 - PhpJens TörnellView Answer on Stackoverflow
Solution 5 - PhpNoFunctionView Answer on Stackoverflow