0

I am creating photo gallery using readdir , in that directory there are non image file also . There are several thousands files in the directory , i am really struggling to filter the extension . Any help will be appreciated

if ($handle = opendir(getcwd())) {

    while (false !== ($entry = readdir($handle))) {
        //but there are other files like doc,pdf,html,php how to fiter them
        echo "<img src='$entry' height='100' width='100'>";
    }


    closedir($handle);
}

2 Answers2

1

Try the glob() function, which allows wildcards for filenames much as you can directly on the command line. e.g.

$files = glob('*.jpg');
foreach($files as $file) {
   echo ....
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
1

Agreed with marc but you can filter various extensions as well with the little trick

foreach (glob('*.{jpg,gif,bmp,jpeg}', GLOB_BRACE) as $filename) {
    echo "<img src=".$filename.">";
}
sumit
  • 15,003
  • 12
  • 69
  • 110