PHP: List files in a directory

This function simply returns an array containing a list of a directory's contents.


function getDirectoryList ($directory) 

  // create an array to hold directory list
  $results = array();

  // create a handler for the directory
  $handler = opendir($directory);

  // open directory and walk through the filenames
  while ($file = readdir($handler)) {

    // if file isn't this directory or its parent, add it to the results
    if ($file != "." && $file != "..") {
      $results[] = $file;


  // tidy up: close the handler

  // done!
  return $results;



Please do get in touch if you have any questions or suggestions for improvement. You might also like my other PHP articles.