Although this code SORTS the filename and then SORTS the extension perfectly locally, I cannot get this to work on any Hosting company's (shared) server - regardless of the PHP version. It simply will not do any sorting on my Host's server. I have tried this on many different hosting companies' servers. So this is not an issue with one hosting company/server. (*FYI: The oldest version of PHP tested was 5.2.17 - most recent tested was 5.3.26. Local = 5.3.5)
OUTPUT 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"; }
/* SORT FILES - First by filename and then by extension */
$files = glob($handle."/*.*");
$files = array_map("basename", $files);
natcasesort($files);
echo '
<div><a href='.$file.' class='.$fileExt.'> </a></div>
<div><a href='.$file.'>$file</a></div>
';
}
closedir($handle);
?>
Please refer to this posting for more info:
PHP - Sort data after original sort