Code

Simple PHP Directory Script

I sometimes have a tendency to create a lot of files 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. 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" ;
}

?>

0 Comments

Be the first to leave a comment.

Leave a Comment

Name
Email
Website
Comment
Name and email are required. Your email will not be published.

This post was published on Saturday, January 10th, 2009 by Robert James Reese in the following categories: HTML, PHP. Before using any of the code or other content in this post, you must read and agree to our Terms & Conditions.

Copyright © 2012, Ink Plant. All rights reserved.