• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 474
  • Last Modified:

PHP readdir(): Sort results alphanumerically

I use the attached code to return a list of directories.  How can I display the results sorted alphanumerically?
if ($handle = opendir('directory')) {
    while (($file = readdir($handle)) !==false) {
        if (substr($file,0,1) != "." && is_dir($file)) {
	echo $file;
        }
    }
    closedir($handle);
}

Open in new window

0
hankknight
Asked:
hankknight
2 Solutions
 
ludofulopCommented:

if ($handle = opendir('directory')) {
$dirs = array()
    while (($file = readdir($handle)) !==false) {
        if (substr($file,0,1) != "." && is_dir($file)) {
	$dirs[] = $file;
        }
    }
    closedir($handle);
    sort($dirs);
    echo implode('<br />', $dirs);
}

Open in new window

0
 
Ray PaseurCommented:
Gather the data into an array and sort the array.
if ($handle = opendir('directory')) 
{
    while (($file = readdir($handle)) !==false) 
    {
        if (substr($file,0,1) != "." && is_dir($file)) 
        {
	    $arr[] = $file;
        }
    }
    closedir($handle);
}

sort($arr);
var_dump($arr);

Open in new window

0
 
Ray PaseurCommented:
@ludofulop: Proof that great minds think alike!  ;-)

~Ray
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Ray PaseurCommented:
@hankknight: Here is a man page worth reading as you choose the exact kind of sort you need.

http://us.php.net/manual/en/array.sorting.php
0
 
darren-w-Commented:
I think ksort may be better?
<?php
$directory = new directoryListing("../testfolder");
print_r($directory->sortListing());

class directoryListing {
   private $_dPath;
   private $_listing = array();
   public function __construct($path) {
      $this->_dPath = $path;
   }
   private function getListing() {
      if ($handle = opendir($this->_dPath)) {
         while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != ".." && !is_dir($file)) {
               $this->_listing[] = $file;
            }
         }
         closedir($handle);
      }
   }
   public function sortListing()
   {
      $this->getListing();
      ksort($this->_listing);
      return $this->_listing;      
   }
}
?>

Open in new window

0
 
Ray PaseurCommented:
Please see:
http://us3.php.net/manual/en/function.ksort.php

There are only numbered keys in the arrays we built here, so the sort order with ksort will not change.
0
 
darren-w-Commented:
Hi Ray,

Sorry, sort does work fine, it must of been the folder structure I was testing it on, one thing I did find was wrong in the previous examples was that  is_dir($file)  needs to be ! is_dir($file)  as it was not returning any listings.

Darren
0
 
Ray PaseurCommented:
I think the OP wanted to sort a list of directories.  That is probably why we found is_dir() in his original post.
0
 
darren-w-Commented:
Yep, know that , that what the class returns...
0
 
hankknightAuthor Commented:
Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now