monkeymynd Posted October 30, 2010 Share Posted October 30, 2010 I must be brain dead...I've searched everywhere and can't seem to find a snippet for this. I'm just looking for some code to show a directory listing, but only certain files (by extension) and I don't want to show any sub-directories. I thought I might have been able to do this in the .htaccess file, but I can't find anything that will get rid of the sub-directories. So, I figure there must be a way to get a file listing, check the extension, add it to the page, or skip it if it's a sub-directory or doesn't match the extension. Or, I could just be totally, way off...anyway, any help would be greatly appreciated...I'm fairly new to php. Quote Link to comment https://forums.phpfreaks.com/topic/217273-show-directory-listing-excluding-sub-directories-and-only-certain-files-by-ext/ Share on other sites More sharing options...
phpfreak Posted October 30, 2010 Share Posted October 30, 2010 This should get you started: <?php if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo "$file\n"; } } closedir($handle); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/217273-show-directory-listing-excluding-sub-directories-and-only-certain-files-by-ext/#findComment-1128285 Share on other sites More sharing options...
trq Posted October 30, 2010 Share Posted October 30, 2010 This would display all files with the .txt extension within the foo directory for example.... <?php foreach (glob("foo/*.txt") as $f) { echo $f . "\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/217273-show-directory-listing-excluding-sub-directories-and-only-certain-files-by-ext/#findComment-1128286 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.