markspec87 Posted September 6, 2006 Share Posted September 6, 2006 Is there anyway PHP can output the filenames of files in a specific folder on my site?:) Quote Link to comment https://forums.phpfreaks.com/topic/19908-show-files-in-a-specific-folder/ Share on other sites More sharing options...
ronverdonk Posted September 6, 2006 Share Posted September 6, 2006 Sure you can. Following lists 3 types of html files.[code]<?php//// An example if you want all of a specific file type in a directory. //$Gdir = "mydir/";foreach (glob("$Gdir/{*.htm,*.html,*.shtml}", GLOB_BRACE) as $file) echo $file.'<br>';?>[/code]Ronald 8) Quote Link to comment https://forums.phpfreaks.com/topic/19908-show-files-in-a-specific-folder/#findComment-87153 Share on other sites More sharing options...
Daniel0 Posted September 6, 2006 Share Posted September 6, 2006 [code]<?phpprint_r(scandir("/etc"));?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19908-show-files-in-a-specific-folder/#findComment-87155 Share on other sites More sharing options...
markspec87 Posted September 6, 2006 Author Share Posted September 6, 2006 Is there anyway i can error catch with that?i.e If no files are found echo "no files were found" ? Quote Link to comment https://forums.phpfreaks.com/topic/19908-show-files-in-a-specific-folder/#findComment-87173 Share on other sites More sharing options...
Daniel0 Posted September 6, 2006 Share Posted September 6, 2006 [code]<?php$items = scandir("/path/to/whatever");if(count($items) < 2){ echo "No files found";}else { foreach($items as $item) { if($item != '.' && $item != '..') { echo "{$item}<br />\n"; } }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19908-show-files-in-a-specific-folder/#findComment-87209 Share on other sites More sharing options...
markspec87 Posted September 6, 2006 Author Share Posted September 6, 2006 oo sigh.My host has php4says that command is php5 :(Is there an alternative? Quote Link to comment https://forums.phpfreaks.com/topic/19908-show-files-in-a-specific-folder/#findComment-87257 Share on other sites More sharing options...
wildteen88 Posted September 6, 2006 Share Posted September 6, 2006 You'll have to scan the directory manually using opendir and readdir. Quote Link to comment https://forums.phpfreaks.com/topic/19908-show-files-in-a-specific-folder/#findComment-87264 Share on other sites More sharing options...
Daniel0 Posted September 6, 2006 Share Posted September 6, 2006 [code]<?php$dir = "/var/www";$dh = @opendir($dir);$content = array(); // to prevent an E_NOTICEwhile($item = @readdir($dh)){ if($item != '.' && $item != '..') { $content[] = $item; }}if(count($content) < 1){ echo "Directory is empty or do not exist";}else { foreach($content as $item) { if(is_dir("{$dir}/{$item}")) { echo "[DIR] "; } echo "{$item}<br />\n"; }}?>[/code]Note that I put it in an array in the while loop so you could manipulate the array. Then I ran them through a loop again later to show you how to output it. You could of course just echo them directly from the while loop. Quote Link to comment https://forums.phpfreaks.com/topic/19908-show-files-in-a-specific-folder/#findComment-87273 Share on other sites More sharing options...
markspec87 Posted September 6, 2006 Author Share Posted September 6, 2006 thanks for that.Managed to get the files found to be links to them aswell.Works a treat :) Quote Link to comment https://forums.phpfreaks.com/topic/19908-show-files-in-a-specific-folder/#findComment-87279 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.