Skip to menu

Skip to content



Listing files stored within a directory using PHP

Tuesday, July 8th, 2008 at 6:23pm, in File Management, written by Tom Foyster

Being able to create lists out of files within a directory quickly and easily is a really good skill to have, especially when creating Content Management and web based File Management Systems.

This method is a really simple version, and can be expanded in a number of ways.

Firstly we create a function to return the formatted file list. When the function is called, we also send through the directory to be used as a string.

function file_list($dir) {

}

We then create a variable to hold our formatted list.

function file_list($dir) {

   $list = ‘’;

}

Then, we need to start off our html list.

function file_list($dir) {

   $list = ‘’;

   $list .= ‘<ul>’ . “\r\n”;

}

Next we need to create a variable to hold our opened directory. To do this we use the opendir() function.

function file_list($dir) {

   $list = ‘’;

   $list .= ‘<ul>’ . “\r\n”;

   $open_file = opendir($dir);

}

Now we have the directory open, we can loop through it’s contents.

function file_list($dir) {

   $list = ‘’;

   $list .= ‘<ul>’ . “\r\n”;

   $opened_file = opendir($dir);

   while ( $file = readdir($opened_file) ) {

   }

}

When we loop through the file, we need to check that the $file variable isn’t this directory or its parent. These are represented by ‘.’ and ‘..’.

function file_list($dir) {

   $list = ‘’;

   $list .= ‘<ul>’ . “\r\n”;

   $opened_file = opendir($dir);

   while ( $file = readdir($opened_file) ) {

      if ( $file != “.” && $file != “..” ) {

      }

   }

}

Now we can enter the files into our list, within the loop.

function file_list($dir) {

   $list = ‘’;

   $list .= ‘<ul>’ . “\r\n”;

   $opened_file = opendir($dir);

   while ( $file = readdir($opened_file) ) {

      if ( $file != “.” && $file != “..” ) {

         $list .= ‘<li>’ . $file . ‘</li>’ . “\r\n”;

      }

   }

}

Then we need to close off our list after the while loop has finished.

function file_list($dir) {

   $list = ‘’;

   $list .= ‘<ul>’ . “\r\n”;

   $opened_file = opendir($dir);

   while ( $file = readdir($opened_file) ) {

      if ( $file != “.” && $file != “..” ) {

         $list .= ‘<li>’ . $file . ‘</li>’ . “\r\n”;

      }

   }

   $list .= ‘<ul>’ . “\r\n”;

}

Then, just to be tidy, we close the open directory, stored in the $opened_file variable. Also, remember to return the finished list!

function file_list($dir) {

   $list = ‘’;

   $list .= ‘<ul>’ . “\r\n”;

   $opened_file = opendir($dir);

   while ( $file = readdir($opened_file) ) {

      if ( $file != “.” && $file != “..” ) {

         $list .= ‘<li>’ . $file . ‘</li>’ . “\r\n”;

      }

   }

   $list .= ‘</ul>’ . “\r\n”;

   closedir($opened_file);

   return $list;

}

Finally, just call the function, passing through the directory you want to list.

$files = file_list(‘images/’);

  • bodytext
  • del.icio.us
  • Facebook
  • Google
  • Reddit
  • Technorati
  • Furl
  • StumbleUpon
  • NewsVine
  • Slashdot
  • Spurl
  • Ma.gnolia
  • Live
  • YahooMyWeb
  • E-mail this story to a friend!

Comments on this article

No Avatar

By chancegarcia, Thursday, August 7th, 2008 at 12:32pm

bah. sorry. here's a html friendly display format.

function listing($dir) {
$doNotWant=array(".","..");
$listing="

    ";
    $files=scandir($dir);
    foreach($files as $file) {
    if (!is_dir($file)&&!in_array($file,$doNotWant)) {
    $listing.="
  • $file
  • ";
    }
    }
    $listing.="
";
return $listing;
}

No Avatar

By chancegarcia, Thursday, August 7th, 2008 at 12:27pm

wouldn't this be a bit cleaner?

function listing($dir) {
$doNotWant=array(".","..");
$listing="

    ";
    $files=scandir($dir);
    foreach($files as $file) {
    if (!is_dir($file)&&!in_array($file,$doNotWant)) {
    $listing.="
  • $file
  • ";
    }
    }
    $listing.="
";
return $listing;
}

I use something along these lines for the same tasks. also, with either method, you can go recursive with is_dir()

Nice tutorial though.

No Avatar

By urbiance, Thursday, July 17th, 2008 at 9:02pm

Nice explanation, thanks.

Please log in to post a comment about this article.


Clear Content - Easy to use PHP 5 Content Management System
CSS Contest

Recommended resources

Recent articles