简体   繁体   中英

creating photo gallery without database

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

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

$files = glob('*.jpg');
foreach($files as $file) {
   echo ....
}

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.">";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM