Solved

PHP readdir(): Sort results alphanumerically

Posted on 2010-09-14
10
466 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
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…

690 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