Skip to content

Instantly share code, notes, and snippets.

@AaronTraas
Created January 15, 2015 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AaronTraas/b4173f41c0694ffc7334 to your computer and use it in GitHub Desktop.
Save AaronTraas/b4173f41c0694ffc7334 to your computer and use it in GitHub Desktop.
PHP simple directory listing scripts
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?/$1 [L]
Options -Indexes
Header set X-UA-Compatible "IE=Edge"
<?php
$query = trim( $_SERVER['REQUEST_URI'] );
$parts = preg_split('@/@', $query, NULL, PREG_SPLIT_NO_EMPTY);
$title = urldecode( $parts[count($parts)-1] );
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $title ?></title>
<style>
html { margin: 0; padding: 0; background-color: #000; color: #fff; }
body { max-width: 800px; margin: 3em auto;}
ul { border-top: 1px solid #fff; padding: 0; margin: 50px 0; }
li { list-style: none; border-bottom: 1px solid #fff; }
li a { display: block; color: #fff; text-decoration: none; line-height: 96px; background-size: auto 64px; background-position: left center; background-repeat: no-repeat }
li a:hover { background-color: #333; text-decoration: underline; }
</style>
</head>
<body>
<h1><?php echo $title; ?></h1>
<ul>
<?php
//echo "<pre>"; print_r( $_SERVER );
// array to hold return value
$retval = array();
$dir = dirname(__FILE__) . $_SERVER['QUERY_STRING'];
// add trailing slash if missing
//if(substr($dir, -1) != "/") $dir .= "/";
// full server path to directory
$d = @dir($dir) or die("getImages: Failed opening directory $dir for reading");
while(false !== ($entry = $d->read())) {
// skip hidden files
if( ($entry[0] == ".") || ($entry == "index.php") ) continue;
if( is_dir( $entry ) )
{
echo "<li><a href=\"$entry\">Album: $entry</a></li>";
}
else
{
echo "<li><a href=\"$entry\" style=\"padding: 0 0 0 96px; background-image: url( './$entry' )\">$entry</a></li>";
}
}
$d->close();
?>
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment