Code

A Simple PHP Directory Script

I have a tendency to create a lot of files sometimes and forget what I have named them. Then, I get super annoyed when I have to stop and look them up because the environment I’m working in doesn’t have a built-in directory feature enabled. So, one day in an effort to combat this annoyance, I decided to slap together this simple PHP script that grabs all files from a given directory of a specified file type(s) and turns them into a nice little on screen directory. To see an example in action, check out index.php. Here's the code:

<?php

$dir = "./" ;
$okfiletypes = array("php","html") ;

$files = array() ;
if (is_dir($dir)) {
  if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
      if (($file != ".") && ($file != "..")) {
        if (in_array(strtolower(substr($file,(strrpos($file,".")+1))),$okfiletypes)) {
          $files[] = $file ;
          }
        }
      }
    closedir($dh);
    }
  }
sort($files);
foreach ($files as $file) {
  echo "<div><a href=\"$file\">$file</a></div>\n" ;
  }

?>
Copyright © 2010, Ink Plant. All rights reserved.