Solved

PHP readdir(): Sort results alphanumerically

Posted on 2010-09-14
10
457 Views
Last Modified: 2012-05-10
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
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 15

Accepted Solution

by:
ludofulop earned 250 total points
ID: 33671180

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
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 33671183
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33671188
@ludofulop: Proof that great minds think alike!  ;-)

~Ray
0
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!

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33671206
@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
 
LVL 13

Expert Comment

by:darren-w-
ID: 33672343
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33672510
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
 
LVL 13

Expert Comment

by:darren-w-
ID: 33672786
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33672817
I think the OP wanted to sort a list of directories.  That is probably why we found is_dir() in his original post.
0
 
LVL 13

Expert Comment

by:darren-w-
ID: 33672950
Yep, know that , that what the class returns...
0
 
LVL 16

Author Closing Comment

by:hankknight
ID: 33773736
Thanks
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question