aussie_okie Posted September 26, 2008 Share Posted September 26, 2008 I have this code but when it displays it is not in order. How can I make this sort a-z? <? //define the path as relative $path = "/staff/files/"; //using the opendir function $dir_handle = @opendir($path) or die("Unable to open $path"); echo "Downloadable Files"; //running the while loop while ($file = readdir($dir_handle)) { if($file!="." && $file!="..") echo "<a href='/files/$file' target='_blank'>$file</a>"; } //closing the directory closedir($dir_handle); ?> thanks Quote Link to comment https://forums.phpfreaks.com/topic/125982-solved-how-to-sort-server-dir-a-z/ Share on other sites More sharing options...
adam291086 Posted September 26, 2008 Share Posted September 26, 2008 i know you can do it with http://uk.php.net/manual/en/function.ftp-nlist.php look at some of the posts below it, someone has posted a fuction with listing them Quote Link to comment https://forums.phpfreaks.com/topic/125982-solved-how-to-sort-server-dir-a-z/#findComment-651472 Share on other sites More sharing options...
CroNiX Posted September 26, 2008 Share Posted September 26, 2008 instead of dumping your output directly, why not store the filename in an array, sort the array, then create the links from the array? <?php $filearray=array(); while ($file = readdir($dir_handle)) { if($file!="." && $file!="..") $filearray[]=$file; //store filenames in array } sort($filearray); //sort the array foreach($filearray as $filename) { //loop through array and output data echo "<a href=\"/files/$filename\" target=\"_blank\">$filename</a>"; } Quote Link to comment https://forums.phpfreaks.com/topic/125982-solved-how-to-sort-server-dir-a-z/#findComment-651479 Share on other sites More sharing options...
aussie_okie Posted September 26, 2008 Author Share Posted September 26, 2008 Perfect thanks! instead of dumping your output directly, why not store the filename in an array, sort the array, then create the links from the array? <?php $filearray=array(); while ($file = readdir($dir_handle)) { if($file!="." && $file!="..") $filearray[]=$file; //store filenames in array } sort($filearray); //sort the array foreach($filearray as $filename) { //loop through array and output data echo "<a href=\"/files/$filename\" target=\"_blank\">$filename</a>"; } Quote Link to comment https://forums.phpfreaks.com/topic/125982-solved-how-to-sort-server-dir-a-z/#findComment-651502 Share on other sites More sharing options...
aussie_okie Posted September 26, 2008 Author Share Posted September 26, 2008 thanks! i know you can do it with http://uk.php.net/manual/en/function.ftp-nlist.php look at some of the posts below it, someone has posted a fuction with listing them Quote Link to comment https://forums.phpfreaks.com/topic/125982-solved-how-to-sort-server-dir-a-z/#findComment-651505 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.