Log in

View Full Version : Sort read directory with PHP



jc_gmk
02-19-2008, 04:31 PM
I have a PHP script that uses opendir() and readdir().

I use a while loop to display the results as a list. This works fine on my localhost but not on my web server (displays them in a random order).

Is there any function I can use to sort the results of the while statement (Alphabetically)?

thetestingsite
02-19-2008, 05:48 PM
In the while loop, place the current file in an array (let's call it the files[] array). After the while loop, you can use sort (http://php.net/sort) or ksort (http://php.net/ksort) and echo each item in the array using a foreach loop.

Hope this helps.

jc_gmk
02-20-2008, 10:26 AM
Perfect, Thanks!