Solved

How can I classifiy alphabetically using opendir ?

Posted on 2007-04-05
5
390 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

914 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

18 Experts available now in Live!

Get 1:1 Help Now