Jump to content

Archived

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

wardo

Reading from a directory

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.

Share this post


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

Share this post


Link to post
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\" /]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
I've tried using glob() but I'm doing something wrong. How do I integrate it into my original code?

Share this post


Link to post
Share on other sites
[!--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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.