tigertim Posted September 28, 2010 Share Posted September 28, 2010 Hi Sorry if this is the wrong place for this but I'm totally stumped here and I know its a basic question but I'm very new to php and I need help :-) Basically all i would like to do is list the files and folders in the following directory alphabetically, I know i need to put them in an array etc but I don't know how and would really appreciate some help! So any help would be very gratefully received. Here's my code so far: <?php $sub = ($_GET['dir']); if (strstr($sub, "..")) {$sub = "";} $path = '../media/Documents'; $path = $path . "$sub"; $dh = opendir($path); $i=1; while (($file = readdir($dh)) !== false) { if(substr($file,0,1) != ".") { if (substr($file, -4, -3) =="."){ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. $file <br />"; }else{ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. <a href='?dir=$sub/$file'>$file</a><br />"; } $i++; } } closedir($dh); ?> Many thanks in advance! Link to comment https://forums.phpfreaks.com/topic/214609-how-do-i-list-foldersfiles-alphabetically-help/ Share on other sites More sharing options...
litebearer Posted September 28, 2010 Share Posted September 28, 2010 Might try this ... http://www.nstoia.com/toh/listdir.php Link to comment https://forums.phpfreaks.com/topic/214609-how-do-i-list-foldersfiles-alphabetically-help/#findComment-1116731 Share on other sites More sharing options...
kickstart Posted September 28, 2010 Share Posted September 28, 2010 Hi Read the files into an array, then use asort to sort the array and then loop through the resulting array. Something like this <?php$sub = ($_GET['dir']);if (strstr($sub, "..")) {$sub = "";}$path = '../media/Documents';$path = $path . "$sub";$dh = opendir($path);$i=1;$fileArray = array();while (($file = readdir($dh)) !== false) {$fileArray[] = $file;}closedir($dh);asort)$fileArray);foreach($fileArray AS $singleFile){if(substr($singleFile,0,1) != ".") { if (substr($singleFile, -4, -3) ==".") { echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. $singleFile <br />"; } else { echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. <a href='?dir=$sub/$singleFile'>$singleFile</a><br />"; } $i++;}}?> All the best Keith Link to comment https://forums.phpfreaks.com/topic/214609-how-do-i-list-foldersfiles-alphabetically-help/#findComment-1116742 Share on other sites More sharing options...
tigertim Posted September 28, 2010 Author Share Posted September 28, 2010 Awesome!!! Thanks very much guys for your help, I've learn alot and everything works, thanks again Link to comment https://forums.phpfreaks.com/topic/214609-how-do-i-list-foldersfiles-alphabetically-help/#findComment-1116753 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.