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! Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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.