Jump to content

Recommended Posts

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

                              <td width='20%' class='titlebar'>&nbsp</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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.