Rich Darvins
Rich Darvins

Reputation: 371

Sorting directories returned from opendir()?

I'm trying to iterate through a list of folders, but I can't seem to figure out an easy way to get opendir() to return sorted records like scandir() can. How can I sort directories opened with opendir()?

Upvotes: 1

Views: 224

Answers (3)

Eduardo Reveles
Eduardo Reveles

Reputation: 2175

Your best option would be to store dir names in an array and then use a sorting function.

<?php
$directories = array();

$dh = opendir('./mydir/');
while ($dir = readdir($dh)) {
    $directories[] = $dir;
}
closedir($dh);

print_r(sort($directories));

Upvotes: 2

GolezTrol
GolezTrol

Reputation: 116110

If scandir fits your needs, you can use it. If for whatever reason you cannot use scandir, you can store the directories in an array and sort that array using one of the many sorting functions.

Upvotes: 4

WisdomPanda
WisdomPanda

Reputation: 129

opendir() does not have a sorting option, sadly.

If you have to have sorting, you will need to use scandir(). :(

Upvotes: 1

Related Questions