Listing the contents on a web server without listing directories

I seem to require this snippet of code quite often.

This PHP code will list the contents of a web server’s directory. It will list only files, and not directories (so it’ll skip the parent directory link which shows up by default on most Apache Index files).

<?php
if ($handle = opendir('.')) {
	while (false !== ($file = readdir($handle))) {
		if ($file != "index.php" && strncmp($file, ".", 1) != 0) {
			echo "<li><a>" . $file . "</a></li>\n";
		}
	}
	closedir($handle);
}
?>

Leave a comment