Solved

How can I classifiy alphabetically using opendir ?

Posted on 2007-04-05
5
392 Views
Last Modified: 2008-01-09
Hello

I have the following code
$path = "/home/seoplatf/cache/directory/";
$r= opendir($path);                              
while ($fichier = readdir($r)){
 if (strpos($fichier,".")>0) {
            $dossier=substr($fichier,strpos($fichier,".")+1);
            $filename=substr($fichier,0,strpos($fichier,"."));                  
            }            
        }              
 closedir($r);

How can I modify this code in order to classifiy alphabetically $filename?

Thanks

regards
0
Comment
Question by:pofpof98
5 Comments
 
LVL 12

Accepted Solution

by:
Jeff Darling earned 500 total points
ID: 18856713
You could store the filenames in an array, then sort the array.

code:

<?php
$path = "./";
$r= opendir($path);
$numfiles=0;
while ($fichier = readdir($r)){
 if (strpos($fichier,".")>0) {
            $dossier=substr($fichier,strpos($fichier,".")+1);
            $filename[$numfiles]=substr($fichier,0,strpos($fichier,"."));
            $numfiles++;
            }
 }

echo "<h1>Before Sort</h1>";
for($i=0;$i<$numfiles;$i++){
 echo "<li>[$i] ".$filename[$i]."</li>";
 }

// Sort the array
sort($filename);

echo "<h1>After sort</h1>";
for($i=0;$i<$numfiles;$i++){
 echo "<li>[$i] ".$filename[$i]."</li>";
 }

 closedir($r);
 ?>
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 18856795
It might be that the php5 directoryiterator has your files sorted by default. It would require a testrun though:

<?php
$dir = new DirectoryIterator("./");
foreach ($dir as $file) {
    if ($dir->isDot()) {
        continue;
    }
    echo $file . "\n";
}
?>

-r-
0
 
LVL 14

Expert Comment

by:raja_ind82
ID: 18856875
What does opendir() sort by as a default????

From: http://us3.php.net/manual/en/function.readdir.php
"The filenames are returned in the order in which they are stored by the filesystem."

A method of sorting:

if ($handle = opendir('audio')) {
 while (false !== ($file = readdir($handle))) {
  if ($file != "." && $file != "..") $thefiles[] = $file;
 }
 closedir($handle);
}

sort($thefiles);

for ($i=0;$i<count($thefiles);$i++) {
echo '&file'.($i+1).'='.$thefiles[$i];
}

echo "&total=$i";

Ref: http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_21364734.html 
0
 

Author Comment

by:pofpof98
ID: 18857350
I have done
$path = "./";
$r= opendir($path);
$numfiles=0;
while ($fichier = readdir($r)){
 if (strpos($fichier,".")>0) {
            $dossier$numfiles]=substr($fichier,strpos($fichier,".")+1);
            $filename[$numfiles]=substr($fichier,0,strpos($fichier,"."));
            $numfiles++;
            }
 }
// Sort the array
sort($filename);
 closedir($r);
but I would like to sort also $dossier because $filename[$numfiles] must correspond to $dossier$numfiles]. HNow can I do?

Thanks

Regards

0
 
LVL 14

Expert Comment

by:raja_ind82
ID: 18861502
you are trying to split the extension. if you want to split the extenstion. split this after doing the the sort. After splitting you can have the sorted array and form a single loop and split the extension in another array and use where ever you want.

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

791 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