Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glob() return in an empty array

Tags:

php

glob

Im trying to set a array containing all the image files in a directory, ive got my index.php set up in the same directory as the images. The code im using is

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

var_dump($images);

which returns - array(0) { } in the browser.. any idea what im doing wrong ?

sorry if this is such an obvious question im still very green to php

like image 383
sam Avatar asked Dec 29 '25 18:12

sam


1 Answers

You should use GLOB_BRACE from PHP doc :

The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells.

GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c'

Example:

$directory = __DIR__;
$images = glob("$directory/*.{jpg,jpeg,png,gif}", GLOB_BRACE);
var_dump($images);
like image 199
Baba Avatar answered Jan 01 '26 08:01

Baba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!