0

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.'>&nbsp;</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.

mar2195
  • 103
  • 1
  • 10

2 Answers2

0

you can use glob along with natcasesort.

$files = glob($directory."/*.*");
// for just the files names 
$files = array_map("basename", $files);
natcasesort($files);

If you wanted to limit the results to items with certain extensions you would change your glob call to

$files = glob($directory."/*.{jpg,png,zip}", GLOB_BRACE);
Orangepill
  • 24,500
  • 3
  • 42
  • 63
  • Awesome! For my education, can you please explain the code just a bit? For instance... "$directory" variable is not in my code - Is your code specific to my code or is this an example? Are you sorting the extensions first and then the filename or ... ? ... and is your code snippet supposed to be entered where my "HERE" is displayed in my example? Thanks. – mar2195 Jul 07 '13 at 04:44
  • the first line just pulls in a list of files almost like if you did a ls or dir command from the command line and returns an array. the second command gets just the filename portion of the filename (glob gives you the path as you expressed it). natcasesort does a "human" sort of an array... at the end you should be able to just walk the array and output your html. – Orangepill Jul 07 '13 at 04:47
  • _the first line just pulls in a list of files almost like if you did a ls or dir command from the command line and returns an array_ ... So the "$directory" variable does not specifically pertain to my code? – mar2195 Jul 07 '13 at 05:05
  • Thank you!! One last thing... Any reason why your sorting would work locally but not on my Host's server? Local PHP Version: 5.3.5 / Server PHP Version: 5.3.26 ... Could this be the issue? – mar2195 Jul 07 '13 at 05:38
  • ... and an error in the line: _$files = array_map(function($a){return basename($a);}, $files);_ ... expecting a ) - - - on another server using PHP Version 5.2.17 ... this is strange. – mar2195 Jul 07 '13 at 05:47
  • shouldn't be natcasesort has been standard equipment on php since 4.x – Orangepill Jul 07 '13 at 05:51
  • the closure on the array map call will give an error on php < 5.3 – Orangepill Jul 07 '13 at 05:52
  • I have absolutely **no sorting at all** on my servers (5.2.17 and 5.3.26) and your code is perfect on my local drive (5.3.5) - exact same code and files! WTF? Losing my mind! It's gotta be the "glob" somehow. – mar2195 Jul 07 '13 at 06:08
0
foreach ($example as $value) {
  $file[] = $value;
//regex would be better incase you have files with more than one period
  list($ext, $nam) = explode('.'$value);
  $extention[] = $ext;
  $name[] = $nam;
}

a_sort($extention);
a_sort($name);


foreach($extention as $key=>$value){
  echo $file[$key]."<br/>";
}

foreach($name as $key=>$value){
  echo $file[$key]."<br/>";
}
Seth McClaine
  • 9,142
  • 6
  • 38
  • 64
  • In regards to my code example ... Would your code above be placed within the WHILE or after the WHILE? – mar2195 Jul 07 '13 at 04:58