Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I classifiy alphabetically using opendir ?

Posted on 2007-04-05
5
Medium Priority
?
398 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 2000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

670 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