Simple script for showing contents of a folder

Long long time ago in a galaxy far far away I wrote a very simple PHP script, that I usually put into index.php of a directory, which contents I’d like to print out to the browser. I’m using this solutions on all servers and hostings which has Apache-based directory listings disabled and thus are showing empty page or error screen, if folder does not contain index.php.

Here’s the code:

[code language=”php”]
$dir = getcwd();

if (is_dir($dir))
{
if ($dh = opendir($dir))
{
echo(‘Files in this folder:<ul>’);

while (($file = readdir($dh)) !== false)
{
if ($file != ‘.’ && $file != ‘..’ && $file != ‘index.php’)
{
echo(‘<li><a href="’.$file.’">’.$file.'</a></li>’);
}
}
closedir($dh);

echo(”);
}
}
[/code]

That’s pretty much everything, folks!

Leave a Reply