3

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

3
  • You probably have to specify the full path. Commented Mar 3, 2013 at 20:53
  • Does this have to be done even if the image files are in the same directory as the script file ? Commented Mar 3, 2013 at 20:55
  • 1
    See the glob() manual page again. Just throwing in commas is not a valid syntax for specifying alternative file extensions. Commented Mar 3, 2013 at 20:56

1 Answer 1

4

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.