Jump to content

How do I list folders/files alphabetically HELP!!!


tigertim

Recommended Posts

I'm pretty new to php and I'm furiously trying to get this code to list the folders/files alphabetically but to no avail! I'd really appreciate it if someone could take a look and guide me as to what to do, I'm pulling what little hair i have left out!

 

I can currently see the folder contents but the order is all over the place.

 

Thanks very much!!!

 

--------------------------

 

<?php

 

$sub = ($_GET['dir']);

if (strstr($sub, "..")) {$sub = "";}

$path = '../media/Movies';

$path = $path . "$sub";

$dh = opendir($path);

$i=1;

while (($file = readdir($dh)) !== false) {

  if(substr($file,0,1) != ".") {

          if (substr($file, -4, -3) =="."){

          echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. $file <br />";

          }else{         

      echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. <a href='?dir=$sub/$file'>$file</a><br />";

        }

 

      $i++;

  }

}

closedir($dh);

 

?>

 

--------------------------

Instead of opendir() try glob(), then use sort() on the resulting array.  You can then loop through the array and do your echos.  Also, instead of the substr() calls, maybe use is_dir() and is_file().

Thanks very much for quick reply!  As I said I'm pretty new to PHP so please bear with me.  Taking your advice, should the code therefore look like this (I'm guessing not as it doesn't seem to work) :'( :

 

--------------------------

 

<?php

 

$sub = ($_GET['dir']);

if (strstr($sub, "..")) {$sub = "";}

$path = '../media/Movies';

$path = $path . "$sub";

$dh = glob($path);

array = sort(a,z)

$i=1;

while (($file = readdir($dh)) !== false) { 

  if(substr($file,0,1) != ".") {

          if (substr($file, -4, -3) =="."){

          echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. $file <br />";

          }else{         

      echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. <a href='?dir=$sub/$file'>$file</a><br />";

        }

 

      $i++;

  }

}

closedir($dh);

 

?>

 

--------------------------

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.