Click to See Complete Forum and Search --> : Displaying a directory


The Anime King
03-02-2004, 04:17 AM
Heyo
I have this code to read a directory:

<?php
$Open = opendir($dir);
while($File = readdir($Open)){
...................

If i display all files in a table there seems to be a random order, but I guess they are sorted by date.
How can I order the array in alphabetical order?
For an example, check http://members.lycos.nl/rpgcontestftp/avatars/dirreader.php?dir=tekken
Greetzzz tAK

clairec666
03-02-2004, 05:19 AM
Try the following:

<?php
$Open = opendir($dir);
while(!(($File = readdir($Open)) == false )){
if (is_dir("$dir/$File")) {
print " Directory: ";
}
else {
print "File: ";
}
print "$File<br />";
}
closedir($dir);
?>

For example if the $dir directory has three files in it, and three directories, the output would be:

Directory: directory 1
Directory: directory 2
Directory: directory 3
File: file1.jpg
File: file2.jpg
File: file3.jpg

If you only wanted to show the files, not the directories, you would use the "print $File" within the if/else loop.

The Anime King
03-02-2004, 05:53 AM
thnx for the code to display the directories, that was going to be my second question :)
the script works, but i still get a random order:

Directory: .
Directory: ..
File: 50cent.jpg
File: tomcruise.jpg
File: madonna.jpg
File: christina.jpg
File: jessica.jpg
File: alizee.jpg

Now is my question, how to get in alphabetical order?
Greetzzz tAK

clairec666
03-02-2004, 06:41 AM
I'm not entriely sure about the alphabetical order.
The best thing would be to put each file name into an array, then sort the array into order. I'll come up with some code for you in a minute!

pyro
03-02-2004, 07:54 AM
Try something like this (note that it uses a case insensitive "natural order" sorting algorithm: natcasesort (http://us2.php.net/manual/en/function.natcasesort.php))

<?php

$path = '/yourdir/'; # absolute path to directory
$handle = opendir($_SERVER['DOCUMENT_ROOT'].$path);

while (false !== ($item = readdir($handle))) {
if (is_dir($_SERVER['DOCUMENT_ROOT'].$path.$item)) {
if ($item != "." && $item != "..") { # strip the . and ..
$directories[] = $item; # directories
}
}
else {
$files[] = $item; # files
}
}
closedir($handle);

natcasesort($directories); # sort the directories using a case insensitive "natural order" algorithm
natcasesort($files); # sort the files using a case insensitive "natural order" algorithm

echo "<h2 style=\"font: large Verdana, Arial, Helvetica, sans-serif; color: #333;\">Directories</h2>\n";
echo "<ul style=\"color: #036; padding-left: 10px; margin-left: 10px; list-style-type: none;\">\n";
foreach($directories as $dir) {
echo "<li>$dir</li>\n";
}
echo "</ul>";

echo "<h2 style=\"font: large Verdana, Arial, Helvetica, sans-serif; color: #333;\">Files</h2>\n";
echo "<ul style=\"color: #036; padding-left: 10px; margin-left: 10px; list-style-type: none;\">\n";
foreach($files as $file) {
echo "<li>$file</li>\n";
}
echo "</ul>";

?>

The Anime King
03-02-2004, 04:29 PM
Thnx for the nice try Pyro, but it doesn't work...
if you ask me, lines like
echo "<li>$file</li>\n";
should be either
print("<li>$file</li>\n");
or
echo "<li>" . $file . "</li>\n";

I dunno why but then still it just types DIRECTORIES and FILES and that's all.
My script works fine, all i need is the directory to be sort...
so all i ask is how to change these lines:
<?php
$Open = opendir($dir);
while($File = readdir($Open)){
...................
so that all files are put into an array.

Greetzzz tAK

pyro
03-02-2004, 04:35 PM
Originally posted by The Anime King
Thnx for the nice try Pyro, but it doesn't work...It did for me. Did you change the $path variable?

Originally posted by The Anime King
if you ask me, lines like
echo "<li>$file</li>\n";
should be either
print("<li>$file</li>\n");
or
echo "<li>" . $file . "</li>\n";I though you were the one doing the asking... Either way, they will all work.