]> git.lizzy.rs Git - dirlistozxa.git/blob - dirlistozxa.php
Add README
[dirlistozxa.git] / dirlistozxa.php
1 <?php
2 $folder = str_replace('?'.$_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);
3 $path = $_SERVER['DOCUMENT_ROOT'].$folder;
4
5 $ignore_file_list = ['.htaccess', 'Thumbs.db', '.DS_Store', 'index.php', 'index.html', 'dirlistozxa.php'];
6
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];
14 }
15
16 function row($name, $date, $size) {
17         return sprintf(
18                 '<tr><td><a href="%s">%s</a></td><td>%s</td><td class="r">%s</td></tr>',
19         $name, $name, $date, $size);
20 }
21
22 function build_blocks($items) {
23         global $ignore_file_list, $path;
24
25         $rtn = '';
26
27         $objects = [ 'directories' => [], 'files' => [] ];
28
29         foreach ($items as $item) {
30                 if ($item == '..' || $item == '.' || in_array($item, $ignore_file_list)) continue;
31
32                 if (is_dir($path.$item))
33                         $objects['directories'][$item] = $item;
34                 else
35                         $objects['files'][$item] = $item;
36         }
37
38         // SORT
39         natsort($objects['directories']);
40         natsort($objects['files']);
41
42         $rtn .= row('../', '-', '-');
43
44         foreach ($objects['directories'] as $dir) {
45                 $name = basename($dir).'/';
46                 $date = date('Y-m-d H:i', filemtime($path.$dir));
47
48                 $rtn .= row($name, $date, '-');
49         }
50
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));
55
56                 $rtn .= row($name, $date, $size);
57         }
58
59         return $rtn;
60 }
61 ?>
62 <!DOCTYPE html>
63 <html>
64 <head>
65         <title>Index of <?=$folder ?></title>
66         <meta charset="utf-8">
67         <style>
68 body {
69         background-color: #111;
70         color: #eee;
71         font-family: monospace;
72         font-size: 12pt;
73         max-width: 1440px;
74         margin: auto;
75         padding: 0 5px;
76 }
77 td { padding: 5px; }
78 th { padding: 0 5px; }
79 .r { text-align: right }
80
81 a {
82         color: lime;
83         text-decoration: none;
84 }
85         </style>
86 </head>
87 <body>
88 <h1>Index of <?=$folder ?></h1>
89
90 <table>
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>
95 </table>
96
97 </body>
98 </html>