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:


$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;

foreach ($files as $file) {
	echo '<div><a href="'.$file.'">'.$file.'</a></div>';


1 Comment

Will Knot B. Revealed Snr.
October 5, 2013, 11:13 pm
Simple, short and sweet. Ideal when only specific files need listing sans folders.

Thanks :)

Leave a Comment

Name and email are required. Your email will not be published.

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 & Conditions.