Forum Moderators: coopster

Message Too Old, No Replies

List files in directory - issue

Not being listed in order

         

divaone

4:04 pm on Dec 6, 2003 (gmt 0)

10+ Year Member



Hi all,

I could use help with a small script. Problem: Some of the files are listed out of numerical order (2,3,1,4,5). Here is the set-up:

One directory with 12 subdirectories, one for each month. Each subdirectory has a subdirectory for each week of that month. Each week's directory has an index.html file.

The script is a basic file lister. The code is placed 12 times in an index.php file to create a sitemap for the main directory. This is one section (of the 12):


echo "<dl><dt><b>Jan 2003</b></dt><dd><table border='0'><tr>";
$jandir="jan2003/";
$jan=opendir($jandir);
while($janb=readdir($jan))
{
$janc = $janb;
$janb = str_replace(".","",$janb);
$janb = str_replace("..","",$janb);
$janb = str_replace("week"," Week ",$janb);
echo "<td><a href=$jandir$janc>$janb</a></td>";
}
closedir($jan);
echo "</tr></table></dd></dl>";

Any particular fixes for this? Also, I've yet to figure out why 'dots' are listed as files if I remove the string replace lines.

TIA :)

Distel

4:15 pm on Dec 6, 2003 (gmt 0)

10+ Year Member



A workaround could be to add a number in front of each entry (e.g.: 01Jan2003, 02Feb2003, ...), add them to an array, use natcasesort() to sort the array, and then extract them again. Not very elegant, but it should work.

ergophobe

4:35 pm on Dec 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not quite sure I follow, but I believe the following two observations should be right.

1. Files are only listed in order when you do a file listing because you apply a sort to the listing after the listing is generated. Alphabetical by filename is only one possible sort. The file system doesn't care at all about what order the file names are in. So you need to get the names and then apply the order.

2. On a *.nix system, there is no real differentiation between a file and a directory. A directory is merely a file that holds information about files. Two pieces of that info are 1) current dir and 2) parent dir and they will be part of the file table and considered valid files (since, well, they are valid files). I don't know how this works on windows, but it may be the same. I just checked and it seems to be the same. When you do a directory listing in Win it gives you the . and the.. so I guess it's generally true.

Tom

coopster

10:22 pm on Dec 7, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Distel and ergophobe have explained what needs to be done, my contribution will be a small code snippet to demonstrate:

// Load up an array with the directory listing:
$dir = 'jan2003/';
if ($handle = @opendir($dir)) {
while (false!== ($filename = readdir($handle))) {
$files[] = $filename;
}
closedir($handle);
natcasesort($files);
// Process the files from the directory listing;
// but skip any files beginning with dots (.):
foreach ($files as $filename) {
if (is_file($dir.$filename) and substr($filename, 0 , 1)!= '.') {
// your statements here...
}
}
}

<edit>cleaned up formatting of lines</edit>

[edited by: coopster at 12:01 am (utc) on Dec. 8, 2003]

divaone

11:28 pm on Dec 7, 2003 (gmt 0)

10+ Year Member



THANK YOU to all three. The natsort and natcasesort info, reading and example have helped a lot. The script(s) is working perfectly now.

Side note: It was still listing the dots for some reason so I've kept my string replace lines for those with no ill effects.

Thanks again!

coopster

12:08 am on Dec 8, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>>Side note: It was still listing the dots for some reason...

Hmm, interesting. I tested this on both freeBSD and Linux servers (Apache 1.3/PHP 4.2.3) and if I print out the $files array, I can see the . and .. files in the array -- however, evaluating it during the loop always eliminates them for me (as well as any .htaccess files or otherwise). What type of configuration are you running?

divaone

2:13 pm on Dec 8, 2003 (gmt 0)

10+ Year Member



yes I found it interesting too. Seems it should work. I'm also on Apache/1.3.27 (Unix) and PHP/4.2.3.