faqts : Computers : Programming : Languages : PHP : Common Problems : Files

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

28 of 30 people (93%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

I am reading the contents of a directory into an array. How can I read them into the array in alphabetical order?

Mar 23rd, 2001 13:30
Ben Udall, Dave Martindale, http://www.php.net/manual/en/class.dir.php http://www.php.net/manual/en/function.natcasesort.php


You can't read the files into the array in alphabetical order, however, 
you can sort the contents of the array after you read all the files.
$file_list = array();
$dir_obj   = dir('.');
while ($item_name = $dir_obj->read())
   $file_list[] = $item_name;
$dir_obj->close();
natcasesort($file_list);
foreach ($file_list as $filename)
   echo htmlentities($filename)."<br />\n";