Skip to content

Instantly share code, notes, and snippets.

@Pupskuchen
Created May 6, 2016 18:28
Show Gist options
  • Save Pupskuchen/4de0da243a7bed57380be590e035d58c to your computer and use it in GitHub Desktop.
Save Pupskuchen/4de0da243a7bed57380be590e035d58c to your computer and use it in GitHub Desktop.
a simple and basic php script to create a directory index of the configured directory
<?php
$conf = array(
// specify the url under which this directory listing runs
'url' => 'http://localhost/dirindex',
// specify the directory that should be listed (relative to this script)
'path' => '.',
// if true, files beginning with a dot (.) will be shown, false hides them
'showHidden' => false,
// page title
'title' => 'directory index',
// if true, tells piwik to track download links
'piwik' => true,
// you may set a custom header if you wish to (this will be in the <head>-area)
'header' => null,
// setting this to true enables error reporting, false disables it (mostly for warnings / just to be sure)
'dev' => true,
);
// end of config
error_reporting($conf['dev'] ? E_ALL : 0);
$index = $conf['url'][strlen($conf['url']) - 1] == '/' ? substr($conf['url'], 0, -1) : $conf['url'];
$url = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$dir = '/' . substr($url, strlen($index) + 1);
$request = ($conf['path'] ? $conf['path'] : '.') . $dir;
$current = $index . ($dir != '/' ? $dir : '');
if (basename($request) == "favicon.ico" && !file_exists($request)) {
$img = 'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAA0AAAANABeWPPlAAAABl0RVh0U29md' .
'HdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAIDSURBVFiF7ddPiM1RFAfwzx0Tg2Sy0QwWYjETVmQzK7GxsJpEVlay0GwxJKyIja2yEFuLiViIk' .
'VJKSSlhM5F/hWRS0jSOxdyfeTPen9+M93qbOXV699f9dr7fe+6599yXIkI7raOt7AsC0AkppVU4ju1YWQX3BQ9xISJ+NltED94hSvhzLI8IzfJOX' .
'MQavMclvK0ish9D2IxhnGhmBj7l1e2vpxTHMu5xszOwOgt51kBoMd+XUjpdYmEf8CAiXjcCFvvb1yADvZhQrlYKn8DRenFTBkJ/RLyspzSltAt7s' .
'aREBrZhU46/MyJG/ysD83HcybEv18K0+iK6lX831gK0WsBkI57OspFSSouxbI4ClhY8KaXuPA6MR0UbrlkDportFB7hl7mdgHr+FVfRVVMANuBpE' .
'0mr+fWqW5BS6jHVfHrxGSdxF9/qZ7y0DeEMdv8jIO/1jUw+hoGI+Ngk4oJjXR7eZ9YW4Er+/oEtLbgbDlVw7pghAEfy+DcGW0A+YLqYzxUHoRBw2' .
'PRdf7YF5GtNd97b6JgtoFA2gtRk8i48yfFfobtibsaxeIEVLVj9tRz/u6mmVzn3Ny2TmtyQsAjnK+pqz2xMJ+7hQAYdTCk1epiUtfUYxNb8PRwRN' .
'6sB5/IonY+PY1+tLKWIKPMsn4+NYRQjEfGmFigVhdAua/s/owUBfwB+6o0GnpS1tAAAAABJRU5ErkJggg==';
$img = base64_decode($img);
header('Content-Type: image/png');
header('Content-Length: ' . strlen($img));
die($img);
}
if (file_exists($request) && is_file($request)) {
header('Content-Description: File Transfer');
header('Content-Type: ' . finfo_file(finfo_open(FILEINFO_MIME_TYPE), $request));
header('Content-Disposition: attachment; filename="' . basename($request) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($request));
readfile($request);
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php
echo $conf['title'];
?></title>
<style type="text/css">
* {
margin: 0;
padding: 0;
outline: 0 none;
background: none;
border: 0 none;
font-family: monospace;
}
body {
padding: 20px;
}
table, tr {
border: 1px solid #ececec;
border-collapse: collapse;
}
th, td {
padding: 5px 15px;
text-align: left;
}
</style><?php
if ($conf['header']) {
echo "\n" . $conf['header'];
}
?>
</head>
<body>
<?php
if (file_exists($request) && is_dir($request) && $listing = scandir($request)) {
array_splice($listing, 0, 2);
echo "<h1>index of " . $dir . "</h1>\n";
if (count($listing)) {
echo '<table>' . "\n";
echo '<tr>';
echo '<th>name</th>';
echo '<th>modified</th>';
echo '<th>size</th>';
echo "<tr>\n";
if (strlen($dir) > 1) {
echo '<tr>';
echo '<td colspan="3">' . display_link($index, up_dir($dir), '..') . '</td>';
echo "<tr>\n";
}
$currentDir = getcwd();
foreach ($listing as $e) {
$file = $request . ($dir != '/' ? '/' : '') . $e;
if (($e[0] == '.' && !$conf['showHidden']) || $currentDir . ($file[0] == '.' ? substr($file, 1) : $file) == __FILE__) continue;
echo '<tr>';
echo '<td>' . display_link($current, $e . (is_dir($file) ? '/' : ''), null, $conf['piwik'] && is_file($file)) . '</td>';
echo '<td>' . date("Y-m-d H:m:s T", filemtime($file)) . '</td>';
echo '<td>' . size(filesize($file)) . '</td>';
echo "<tr>\n";
}
echo "</table>\n";
} else {
if (strlen($dir) > 1) {
echo display_link($index, up_dir($dir), '..');
}
}
} else {
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found", true, 404);
echo '<h1>404</h1>';
echo display_link($index, '/', 'back to start');
}
function up_dir($dir)
{
$dirs = array_values(array_filter(explode('/', $dir)));
$dir_count = count($dirs);
return substr($dir, 0, -(($dir_count ? strlen($dirs[$dir_count - 1]) : 0) + 1));
}
function size($bytes)
{
$identifiers = ['B', 'K', 'M', 'G', 'T'];
$identifier = 0;
while ($bytes / 1024 > 1) {
$identifier++;
$bytes /= 1024;
}
return ($bytes ? round($bytes, 3) : '0') . $identifiers[$identifier];
}
function display_link($index, $link, $display = null, $piwik_download = false)
{
if (!$link) $link = "";
$url = $index . ($index[strlen($index) - 1] != '/' && !($link && $link[0] == '/') ? '/' : '') . $link;
$display = htmlspecialchars($display ? $display : $link, ENT_QUOTES, 'UTF-8');
return '<a href="' . $url . '"' . ($piwik_download ? ' class="piwik_download"' : '') . '>' . $display . '</a>';
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment