PHP: Get a directory listing.

Posted October 2nd, 2010


List files within a directory while ignoring system folders such as "Thumbs.db" as well as "." and "..".

<?php
function listFiles($dir){
  if(is_dir($dir)){
    if($handle = opendir($dir)){
      while(($file = readdir($handle)) !== false){
        if($file != "." && $file != ".." && $file != "Thumbs.db"{
          echo '<a target="_blank" href="'.$dir.$file.'">'.$file.'</a><br/>'."\n";
        }
      }
      closedir($handle);
    }
  }
}
?>