I am displaying a directory listing. I'm trying to get the files sorted by filename but also be sorted by their extension.
Before display...
- sort by filename first
- then sort by extension within this sort
Example:
a_first.jpg
a_first.png
a_first.zip
b_second.doc
b_second.gif
b_second.jpg
<?php
function getFileExt($filename) {
return substr(strrchr($filename,'.'),1);
}
$handle=opendir(dirname(__FILE__));
while (($file = readdir($handle))!==false) {
$fileExt = strtolower(getFileExt($file));
if(in_array($file, $ignore_file_list)) { continue; }
if(in_array($fileExt, $ignore_ext_list)) { continue; }
if(is_dir($file)) { $fileExt = "dir"; }
/*
HERE:
Before display...
- sort by filename first
- then sort by extension within this sort
Example:
a_first.jpg
a_first.png
a_first.zip
b_second.doc
b_second.gif
b_second.jpg
*/
echo '
<div><a href='.$file.' class='.$fileExt.'> </a></div>
<div><a href='.$file.'>$file</a></div>
';
}
closedir($handle);
?>
I'm not sure if I need to do the second sort (= the files' extensions) within the WHILE using a FOREACH or do the FOREACH outside of the WHILE (to put the data into an array first) or is this a multi-array sorting situation or ... ???
FYI: Using a database for this is not an option.
Any help is appreciated.