Solved

PHP readdir(): Sort results alphanumerically

Posted on 2010-09-14
10
422 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
10 Comments
 
LVL 15

Accepted Solution

by:
ludofulop earned 250 total points
Comment Utility

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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
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 108

Expert Comment

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

~Ray
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
@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-
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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-
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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-
Comment Utility
Yep, know that , that what the class returns...
0
 
LVL 16

Author Closing Comment

by:hankknight
Comment Utility
Thanks
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now