Search code examples
phpglob

PHP - Glob multiple file extensions only displays first extension listed


I have both jpg and gif images in my directory and I am trying to display them both.

However, below will only display gif:

$pictures = glob("images/*.{gif,jpg}", GLOB_BRACE); 

And this below will only display jpg:

$pictures = glob("images/*.{jpg,gif}", GLOB_BRACE);

Here is the whole thing I am working with:

<?php
$pictures = glob("images/*.{gif,jpg}", GLOB_BRACE); 
for( $i=0; $i<=10; $i++ ){ 
echo "<img src=\"".$pictures[$i]."\" />"; 
}  
?>

I have also tried with an absolute path and had no such luck displaying both. What might be the problem?

Thanks in advance.


Solution

  • You seem to be limiting your search to the first 10 matches. If there are more than ten of each, then you will get them in the order you specified (since they are sorted by how they are found, not alphabetically).

    You could use a foreach loop to iterate through all files, or you could add sort($pictures) before the loop.