Jump to content

Reading from a directory


wardo

Recommended Posts

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:

[code]<?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);
}
?> [/code]

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

Thanks.
Link to comment
https://forums.phpfreaks.com/topic/12133-reading-from-a-directory/
Share on other sites

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\" /]
Have you tried glob() on your machine? If it doesn't sort them A-Z, you could try this:
[code]<?php
$files = array();
foreach (glob("*.*") as $filename) {
    $files[] .= $filename;
}
sort($files);
foreach($files as $file) {
    echo $file."<br/>";
}
?>[/code]
[!--quoteo(post=384565:date=Jun 16 2006, 03:25 PM:name=wardo)--][div class=\'quotetop\']QUOTE(wardo @ Jun 16 2006, 03:25 PM) [snapback]384565[/snapback][/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]
[code]<?php
$files = array();
foreach (glob("departments/policiesnew/docs/policies/atoz/*.*") as $filename) {
    $files[] .= $filename;
}
sort($files);
foreach($files as $file) {
    echo $file."<br/>";
}
?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.