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...
$files = array_filter(glob('path/*. *'), function ($filename) { return preg_match('/\. (jpe? g|png|gif)$/i', $filename); }); sort($files);
A file name may have no extensions. Sometimes it is said to have more than one extension, although terminology varies in this regard, and most authors define extension in a way that doesn't allow more than one in the same file name. More than one extension usually represents nested transformations, such as files. tar.
php file extension refers to the name of a file with a PHP script or source code that has a ". PHP" extension at the end of it. It's similar to a Word file with a . doc file extension.
$files = glob("*.{jpg,png,gif}", GLOB_BRACE);
This is just an expansion of @Jeroen answer.
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);
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
If you have to list out all the files but without folder, you can use this
$files = glob("*.{*}", GLOB_BRACE);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With