Re: Sorting a directory
| From: | Jason Lavigne | Date: | Fri, 08 Dec 2000 00:51:06 +0000 |
| Subject: | Re: Sorting a directory | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-29119@lists.php.net to get a copy of this message | ||
Here is some code taken from the PHP site,
http://www.php.net/manual/function.readdir.php
, it shows how to read in a
directory then sort it. You will be left with a sorted array that you can
then output. The sort function, http://www.php.net/manual/function.sort.php
, has some options to make your life easier. Your other two options would be
to do your own code to sort but it would require you to have your data in an
array anyways, so use the function. Your other option would be to do an exec
or system call and do an ls with the flags to sort the way you want, but I
would not recommend (and everyone else here would agree) doing system or
exec calls when there is another solution (see below).
function GetDirArray($sPath)
{
//Load Directory Into Array
$handle=opendir($sPath);
while ($file = readdir($handle))
$retVal[count($retVal)] = $file;
//Clean up and sort
closedir($handle);
sort($retVal);
return $retVal;
}
Jay
----- Original Message -----
From: "Guneshwar Mayanglambam" <guneshw@makalumedia.com>
To: <php-general@lists.php.net>; <php-dev@lists.php.net>
Cc: <stig@php.net>
Sent: Thursday, December 07, 2000 2:44 PM
Subject: [PHP] Sorting a directory
Hello Everyone,
I am just stuck up with sorting a directory.
when I use the readdir function I get an unsorted list of directory or
files. How can I get a sorted list .
Can anybody help me .
Thank's in advance.
Guneshwar.M