You saw the subject.I'm reading a directory's contents with readdir(), and need to sort the file-names alphabetically, if that's possible.
Any simple way to do this?
| SitePoint Sponsor |





You saw the subject.I'm reading a directory's contents with readdir(), and need to sort the file-names alphabetically, if that's possible.
Any simple way to do this?
Check out the code in this thread (particualry freddydoesphp's code and my posts.
http://www.sitepointforums.com/showt...5&pagenumber=2





Chris that thread maybe overkill for what you need, so here is a basic exmaple, you probably won't even need to sort them since the directoy is listed in alphabetical order already and so your array shouldalreay be alphabetized, but I threw it in for extra care. BTW use dir() instead of readdir() like I did.
PHP Code:<?
$dir = dir(".");
while($file = $dir->read()) {
if (($file != ".") && ($file != "..")) {
$files[] = $file;
}
}
sort($files);
foreach($files as $key => $val) {
print "$val<br>";
}
?>
Please don't PM me with questions.
Use the forums, that is what they are here for.





My mistake -- I am using dir(), and then readdir(), I think. I dunno, I'm confused.
Thanks for your help, both Louie and Sid. I think I'm going to work-around this with SQL -- much easier for me in the long-run, but I am definintely going to save that code Freddy -- I'm sure it will come in handy.
You the man - thanks!
Hi, sorry, I just linked to that thread because I was literally heading off to bed and was too lazy to think straight![]()
Bookmarks