PHP file listing multiple file extensions

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

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

Tags:

Php