4 // List of filenames (and folder names) that should be ignored.
6 '.htaccess', '.htpasswd', // Apache junk files
7 'Thumbs.db', '.DS_Store', // OS junk files
8 'index.php', 'index.html', // Potential other index files
9 '.git', 'vendor', // Dev
10 'dirlistozxa.php', '.dirlistozxa', '.thumbs', 'gen-thumbs' // dirlistozxa
15 // Lazy sanitisation done if the web server somehow sends idiotic input,
16 // nginx with default configuration (merge_slashes) doesn't actually need this.
17 $_SERVER['REQUEST_URI'] = str_replace('../', '', $_SERVER['REQUEST_URI']);
19 $folder = str_replace('?'.$_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);
20 $path = $_SERVER['DOCUMENT_ROOT'].$folder;
22 if (!is_dir($path)) die('invalid folder?');
24 define('THUMB_FOLDER', 1);
25 define('THUMB_FILE', 2);
26 define('THUMB_IMAGE', 3);
28 function display_size($bytes, $precision = 2) {
29 $units = ['B', 'K', 'M', 'G', 'T'];
30 $bytes = max($bytes, 0);
31 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
32 $pow = min($pow, count($units) - 1);
33 $bytes /= (1 << (10 * $pow));
34 return round($bytes, $precision) . $units[$pow];
37 function row($name, $date, $size, $thumb) {
38 $img = match ($thumb) {
39 THUMB_FOLDER => '/.dirlistozxa/folder.png',
40 THUMB_FILE => '/.dirlistozxa/file.png',
41 THUMB_IMAGE => "/.thumbs/".$name,
45 '<tr><td class="tum"><img src="%s" loading="lazy"></td><td><a href="%s">%s</a></td><td>%s</td><td class="r">%s</td></tr>',
46 $img, $name, $name, $date, $size);
49 function build_blocks($items) {
50 global $ignore_file_list, $path, $folder;
54 $objects = [ 'directories' => [], 'files' => [] ];
56 foreach ($items as $item) {
57 if ($item == '..' || $item == '.' || in_array($item, $ignore_file_list)) continue;
59 if (is_dir($path.$item))
60 $objects['directories'][$item] = $item;
62 $objects['files'][$item] = $item;
66 natsort($objects['directories']);
67 natsort($objects['files']);
70 $rtn .= row('../', '', '', THUMB_FOLDER);
72 foreach ($objects['directories'] as $dir) {
73 $name = basename($dir).'/';
74 $date = date('Y-m-d H:i', filemtime($path.$dir));
76 $rtn .= row($name, $date, '-', THUMB_FOLDER);
79 foreach ($objects['files'] as $file) {
80 $name = basename($file);
81 $date = date('Y-m-d H:i', filemtime($path.$file));
82 $size = display_size(filesize($path.$file));
84 $doThumb = file_exists($_SERVER['DOCUMENT_ROOT']."/.thumbs/".$file) ? THUMB_IMAGE : THUMB_FILE;
86 $rtn .= row($name, $date, $size, $doThumb);
95 <title>Index of <?=$folder ?></title>
96 <meta charset="utf-8">
99 background-color: #111;
101 font-family: monospace;
108 th { padding: 0 5px; }
109 .r { text-align: right }
113 text-decoration: none;
129 <h1>Index of <?=$folder ?></h1>
132 <tr><th></th><th>Name</th><th>Last modified</th><th>Size</th></tr>
133 <tr><th colspan="4"><hr></th></tr>
134 <?=build_blocks(scandir($path)) ?>
135 <tr><th colspan="4"><hr></th></tr>
138 <address><?=$_SERVER['SERVER_SOFTWARE'] ?? 'Cool' ?> server at <?=$_SERVER['HTTP_HOST'] ?>, index powered by <a href="https://github.com/rollerozxa/dirlistozxa/">dirlistozxa</a></address>