[email protected] Posted January 2, 2008 Share Posted January 2, 2008 I have a simple application that uses the DirectoryIterator function to retrieve file information from a specified directory and builds a table of name, size and date, sortable on each column. I thought I had this working but upon closer inspection I see that the multiarray sort function is only correct when I sort by filesize. Any help would be greatly appreciated... Here's the code: <?php $sortby = (isset($_GET['sortby'])) ? $_GET['sortby'] : 'file' ; foreach(new DirectoryIterator($currentdir) as $file){ if (!$file->isDot() && $file != "index.php" && substr($file,0,1) != "_"){ $dir_array[] = $file->getFilename(); $size_array[] = $file->getSize(); $time_array[] = $file->getMTime(); } } //COUNT AND SORT switch($sortby) { case "file": $index_count = count($dir_array); array_multisort($dir_array, SORT_ASC); break; case "size": $index_count = count($size_array); array_multisort($size_array, SORT_ASC); break; case "date": $index_count = count($time_array); array_multisort($time_array, SORT_DESC); break; } //LOOP THROUGH ARRAY if($showdotdot=='true') { echo " <tr> <td width='65%' class='titlebar'><div align='left'><a href='filelist2.php?newdir=$previousdir' class='titlebar'> <img src='images/back.gif' width='17' heigt='17'></a></div></td> <td width='15%' class='titlebar'> </td> <td width='20%' class='titlebar'> </td> </tr>"; } for($index=0; $index < $index_count; $index++){ if (is_dir($currentdir."/".$dir_array[$index])) { $newrow = "<tr>"; $newrow .= "<td><img src='images/folder.gif'> <a href='filelist2.php?newdir=$currentdir/$dir_array[$index]' class='filelist'>$dir_array[$index]</a></td>"; $newrow .= "<td> </td>"; $newrow .= "<td> </td>"; $newrow .= "</tr>"; echo $newrow; } elseif (is_file($currentdir."/".$dir_array[$index])) { $extension = strrev(substr(strrev($dir_array[$index]), 0, stripos(strrev($dir_array[$index]), '.'))); switch($extension) { case "jpg": $extimg = "images/image.gif"; break; case "mov": $extimg = "images/mov.gif"; break; default: $extimg = "images/file.gif"; break; } $newrow = "<tr>"; $newrow .= "<td><img src=".$extimg.">"; $newrow .= "<a href='$currentdir/$dir_array[$index]' class='filelist' target='_blank'>$dir_array[$index]</a></td>"; $newrow .= "<td>".number_format($size_array[$index]/1024,2)." Kb</td>"; $newrow .= "<td>".date('d M Y H:i:s', $time_array[$index])."</td>"; $newrow .= "</tr>"; echo $newrow; } } ?> Link to comment https://forums.phpfreaks.com/topic/84150-sort-directoryiterator-array-data/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.