Jump to content

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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.