Search code examples
phpglob

glob() return in an empty array


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


Solution

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