Solved

How can I classifiy alphabetically using opendir ?

Posted on 2007-04-05
5
394 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 13

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

710 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