Jump to content


Photo

Reading from a directory


  • Please log in to reply
6 replies to this topic

#1 wardo

wardo
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 16 June 2006 - 08:21 AM

Hi,

I've got some code that reads the contets of a directory but it orders the files in some random order and I need them displayed a - z. This is the code I have:

<?php
if ($handle = opendir("departments/policiesnew/docs/policies/atoz")) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {
           echo "<li><a href=\"departments/policiesnew/docs/policies/atoz/$file\">$file</a></li><p />";
       }
   }
   closedir($handle);
}
?>

How could i modify this to make it read the files ordered by their name?

Thanks.
www.realdesignnetwork.com

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 16 June 2006 - 09:56 AM

glob() orders the files as they are in the directory, alphabeticaly on my XP machine it appears...
<?php
foreach (glob("*.*") as $filename) {
    echo "$filename size ".filesize($filename)."<br/>";
}
?>


#3 wardo

wardo
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 16 June 2006 - 01:07 PM

Is there any way of arranging them a-z no matter what becuase i need them to be displayed that way even if the users directory is arranging them differently.

Its really strange because i've used this same code before on the same directory and it worked perfectly then [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]
www.realdesignnetwork.com

#4 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 16 June 2006 - 01:19 PM

Have you tried glob() on your machine? If it doesn't sort them A-Z, you could try this:
<?php
$files = array();
foreach (glob("*.*") as $filename) {
    $files[] .= $filename;
}
sort($files);
foreach($files as $file) {
    echo $file."<br/>";
}
?>


#5 wardo

wardo
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 16 June 2006 - 02:25 PM

I've tried using glob() but I'm doing something wrong. How do I integrate it into my original code?
www.realdesignnetwork.com

#6 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 16 June 2006 - 02:30 PM

[!--quoteo(post=384565:date=Jun 16 2006, 03:25 PM:name=wardo)--][div class=\'quotetop\']QUOTE(wardo @ Jun 16 2006, 03:25 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I've tried using glob() but I'm doing something wrong. How do I integrate it into my original code?
[/quote]
<?php
$files = array();
foreach (glob("departments/policiesnew/docs/policies/atoz/*.*") as $filename) {
    $files[] .= $filename;
}
sort($files);
foreach($files as $file) {
    echo $file."<br/>";
}
?>


#7 wardo

wardo
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 16 June 2006 - 02:53 PM

Thanks for your help. I've got it working now using the original code. For some reason it works fine on the live server but not the test server.
www.realdesignnetwork.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users