Solved

How can I classifiy alphabetically using opendir ?

Posted on 2007-04-05
5
389 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 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…

743 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

13 Experts available now in Live!

Get 1:1 Help Now