List contents of a directory

A quick and dirty way to list the contents of a directory. You might want to add more properties to it for displaying only certain file types and so on. I might post that later on.

PHP:
  1. function dirList($directory) {
  2.   $results = array();
  3.   $handler = opendir($directory);
  4.   while ($file = readdir($handler)) {
  5.     if ($file != '.' && $file != '..')
  6.       $results[] = $file;
  7.   }
  8.   closedir($handler);
  9.   return $results;
  10. }

And how to use it:

PHP:
  1. $arr = dirList("./");
  2. for ($i=0; $i<count ($arr); $i++) {
  3.   echo '<a href="'.$arr[$i].'">'.$arr[$i].'</a><br />';
  4. }

3 Responses to “List contents of a directory”
  1. Scott Says:

    You are missing the closing . As well, how do you think you would add filesize to this in a friendly manner?

  2. Scott Says:

    That last message should have said the close A HREF tag, but the html catcher removed the a.

  3. SubZane Says:

    thanks for the heads-up on the a-href tag. Try using the filesize() method for retreiving filesize for the files.

Leave a Reply

You must be logged in to post a comment.