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>';
}

?>

Comments

Loading…

This post was first published on January 10th, 2009 and last updated on March 13th, 2014 by Robert James Reese in the following categories: HTML and PHP. Before using any of the code or other content in this post, you must read and agree to our terms of use.