Listing files stored within a directory using PHP - PHP Tutorials

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/’);

Share/Save/Bookmark

Tags: ,

One Response to “Listing files stored within a directory using PHP - PHP Tutorials”

  1. [...] « Listing files stored within a directory using PHP - PHP Tutorials [...]

Leave a Reply