2 $folder = str_replace('?'.$_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);
3 $path = $_SERVER['DOCUMENT_ROOT'].$folder;
5 $ignore_file_list = ['.htaccess', 'Thumbs.db', '.DS_Store', 'index.php', 'index.html', 'dirlistozxa.php'];
7 function display_size($bytes, $precision = 2) {
8 $units = ['B', 'K', 'M', 'G', 'T'];
9 $bytes = max($bytes, 0);
10 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
11 $pow = min($pow, count($units) - 1);
12 $bytes /= (1 << (10 * $pow));
13 return round($bytes, $precision) . $units[$pow];
16 function row($name, $date, $size) {
18 '<tr><td><a href="%s">%s</a></td><td>%s</td><td class="r">%s</td></tr>',
19 $name, $name, $date, $size);
22 function build_blocks($items) {
23 global $ignore_file_list, $path;
27 $objects = [ 'directories' => [], 'files' => [] ];
29 foreach ($items as $item) {
30 if ($item == '..' || $item == '.' || in_array($item, $ignore_file_list)) continue;
32 if (is_dir($path.$item))
33 $objects['directories'][$item] = $item;
35 $objects['files'][$item] = $item;
39 natsort($objects['directories']);
40 natsort($objects['files']);
42 $rtn .= row('../', '-', '-');
44 foreach ($objects['directories'] as $dir) {
45 $name = basename($dir).'/';
46 $date = date('Y-m-d H:i', filemtime($path.$dir));
48 $rtn .= row($name, $date, '-');
51 foreach ($objects['files'] as $file) {
52 $name = basename($file);
53 $date = date('Y-m-d H:i', filemtime($path.$file));
54 $size = display_size(filesize($path.$file));
56 $rtn .= row($name, $date, $size);
65 <title>Index of <?=$folder ?></title>
66 <meta charset="utf-8">
69 background-color: #111;
71 font-family: monospace;
78 th { padding: 0 5px; }
79 .r { text-align: right }
83 text-decoration: none;
88 <h1>Index of <?=$folder ?></h1>
91 <tr><th>Name</th><th>Last modified</th><th>Size</th></tr>
92 <tr><th colspan="3"><hr></th></tr>
93 <?=build_blocks(scandir($path)) ?>
94 <tr><th colspan="3"><hr></th></tr>