views:

51

answers:

5

I'm using the following PHP code to list all files and folders under the current directory:

<?php
$dirname = ".";
$dir = opendir($dirname);
?>

<?php
while(false != ($file = readdir($dir)))
{
if(($file != ".") and ($file != "..") and ($file != "index.php"))
{
echo("<a href='$file'>$file</a> <br />");
}
}
?>

The problem is list is not ordered alphabetically (perhaps it's sorted by creation date? I'm not sure). How can I make sure it's sorted alphabetically?

+5  A: 

The manual clearly says that:

readdir
Returns the filename of the next file from the directory. The filenames are returned in the order in which they are stored by the filesystem.

What you can do is store the files in an array, sort it and then print it's contents as:

$files = array();
$dir = opendir('.'); // open the cwd..also do an err check.
while(false != ($file = readdir($dir))) {
        if(($file != ".") and ($file != "..") and ($file != "index.php")) {
                $files[] = $file; // put in array.
        }   
}

natsort($files); // sort.

// print.
foreach($files as $file) {
        echo("<a href='$file'>$file</a> <br />\n");
}
codaddict
This is the first time I use PHP. I only need it to list some stuff I uploaded to my apache. Could you please show me how to store the files in an array and sort it?
David B
+1 for natsort() might even want to think about natcasesort()
Bobby Jack
@David you could use `$files = glob("/your/path/*");`
Pekka
@Pekka: that won't return hidden files. Other than the obvious 'navigation pseudo files', the question doesn't suggest whether hidden files are needed, so it's probably safer to assume they are until told otherwise.
Bobby Jack
@David B: By the way: Filenames can contain HTML-Metacharacters. So one should escape them before outputting. Also, you want to check the files being uploaded for validity. Not that one can upload php files or so, which will be executed then..
Jan.
@Bobby good point. However this seems to be possible to circumvent: http://www.php.net/manual/en/function.glob.php#68869
Pekka
@Pekka: Just verified that `glob` gives **sorted** output. +1 to you.
codaddict
@Pekka: blimey, looks ugly, but I guess it works!
Bobby Jack
@cod it does? That comes as a surprise! :) I rather meant glob() to fetch the data needed for your sorting suggestion. (I would still sort it explicitly, the behaviour could vary across systems.)
Pekka
@Pekka: `glob` uses an option `GLOB_NOSORT` which tells it to return unsorted output. So looks like it's default behavior is to give sorted output.
codaddict
For some reason, this doesn't print anything. I added `$dir= ".";` at the beginning (it's missing) but still nothing.
David B
@David: you need to use a `opendir` before you do a `readdir`
codaddict
@David: I've updated my program, to have an `opendir`.
codaddict
@codaddict yeah, I just got it. Thanks!
David B
+1  A: 

Using glob and sort it should work.

Aif
no need to sort() if you use glob() because it sorts alphabetically by default
Mark Baker
+2  A: 

You could put all the directory names inside an array like:

$array[] = $file; 

After that you can sort the array with:

sort($array); 

And then print the links with that content.

I hope this help.

cored
+2  A: 
<?php
$dirname = ".";
$dir = opendir($dirname);

while(false != ($file = readdir($dir)))
{
if(($file != ".") and ($file != "..") and ($file != "index.php"))
{
  $list[] = $file;
}
}

sort($list);

foreach($list as $item) {
echo("<a href='$item'>$item</a> <br />");
}
?>
Andrew Sledge
As a good practice, add `$list = array();` before using it.
Salman A
+1 Thanks Andrew, this works.
David B
A: 

I'd recommend moving away from the old opendir()/readdir(). Either use glob() or if you encounter a lot of files in a directory then use the DirectoryIterator Class(es):

http://www.php.net/manual/en/class.directoryiterator.php http://www.php.net/manual/en/function.glob.php

Regards

Jan.
Jan, What's the reason to not use openddir/readdir? Neither is deprecated.
Bobby Jack
because OOP is the way to go. Also the Iterator-classes have a lot of features implemented build-into php, which you would have to code manually when you use opendir(). So I believe this would also be better for performance or security reasons.
Jan.
http://en.wikipedia.org/wiki/No_Silver_Bullet :)
Bobby Jack