andy182 Posted October 4, 2008 Share Posted October 4, 2008 I have made the following code to list the files in a directory, but wondering if there's any chance you could help me and explain how to list them in alphabetical order. <?php $path = "tabs/"; $dir = opendir($path); echo "<ul>\n"; while($file = readdir($dir)) { if ($file != "." && $file != ".." && $file != ".php") { list ($name, $ext) = explode (".", $file); echo "<li><a href = \"" .$path.$file. "\" target = \" _blank\"> ".$name." </a></li> \n"; } } echo "</ul>\n"; closedir($dir) ?> Link to comment https://forums.phpfreaks.com/topic/127036-solved-loop-directory-in-alphabetical-order/ Share on other sites More sharing options...
zq29 Posted October 4, 2008 Share Posted October 4, 2008 You could add them to an array while looping then sort the array with sort(), alternatively, you could use glob() to fetch the directory contents - It lists them alphabetically be default, I believe. Link to comment https://forums.phpfreaks.com/topic/127036-solved-loop-directory-in-alphabetical-order/#findComment-657117 Share on other sites More sharing options...
andy182 Posted October 4, 2008 Author Share Posted October 4, 2008 Thanks for your help, looping them in a array did the trick. Link to comment https://forums.phpfreaks.com/topic/127036-solved-loop-directory-in-alphabetical-order/#findComment-657123 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.