wardo Posted June 16, 2006 Share Posted June 16, 2006 Hi,I've got some code that reads the contets of a directory but it orders the files in some random order and I need them displayed a - z. This is the code I have:[code]<?phpif ($handle = opendir("departments/policiesnew/docs/policies/atoz")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo "<li><a href=\"departments/policiesnew/docs/policies/atoz/$file\">$file</a></li><p />"; } } closedir($handle);}?> [/code]How could i modify this to make it read the files ordered by their name?Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/12133-reading-from-a-directory/ Share on other sites More sharing options...
zq29 Posted June 16, 2006 Share Posted June 16, 2006 glob() orders the files as they are in the directory, alphabeticaly on my XP machine it appears...[code]<?phpforeach (glob("*.*") as $filename) { echo "$filename size ".filesize($filename)."<br/>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12133-reading-from-a-directory/#findComment-46215 Share on other sites More sharing options...
wardo Posted June 16, 2006 Author Share Posted June 16, 2006 Is there any way of arranging them a-z no matter what becuase i need them to be displayed that way even if the users directory is arranging them differently.Its really strange because i've used this same code before on the same directory and it worked perfectly then [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /] Quote Link to comment https://forums.phpfreaks.com/topic/12133-reading-from-a-directory/#findComment-46252 Share on other sites More sharing options...
zq29 Posted June 16, 2006 Share Posted June 16, 2006 Have you tried glob() on your machine? If it doesn't sort them A-Z, you could try this:[code]<?php$files = array();foreach (glob("*.*") as $filename) { $files[] .= $filename;}sort($files);foreach($files as $file) { echo $file."<br/>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12133-reading-from-a-directory/#findComment-46254 Share on other sites More sharing options...
wardo Posted June 16, 2006 Author Share Posted June 16, 2006 I've tried using glob() but I'm doing something wrong. How do I integrate it into my original code? Quote Link to comment https://forums.phpfreaks.com/topic/12133-reading-from-a-directory/#findComment-46268 Share on other sites More sharing options...
zq29 Posted June 16, 2006 Share Posted June 16, 2006 [!--quoteo(post=384565:date=Jun 16 2006, 03:25 PM:name=wardo)--][div class=\'quotetop\']QUOTE(wardo @ Jun 16 2006, 03:25 PM) [snapback]384565[/snapback][/div][div class=\'quotemain\'][!--quotec--]I've tried using glob() but I'm doing something wrong. How do I integrate it into my original code?[/quote][code]<?php$files = array();foreach (glob("departments/policiesnew/docs/policies/atoz/*.*") as $filename) { $files[] .= $filename;}sort($files);foreach($files as $file) { echo $file."<br/>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12133-reading-from-a-directory/#findComment-46272 Share on other sites More sharing options...
wardo Posted June 16, 2006 Author Share Posted June 16, 2006 Thanks for your help. I've got it working now using the original code. For some reason it works fine on the live server but not the test server. Quote Link to comment https://forums.phpfreaks.com/topic/12133-reading-from-a-directory/#findComment-46283 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.