forked from halgatewood/file-directory-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
237 lines (195 loc) · 8.08 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/*
Free PHP File Directory Listing Script - Version 1.6
The MIT License (MIT)
Copyright (c) 2015 Hal Gatewood
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*** OPTIONS ***/
// TITLE OF PAGE
$title = "List of Files";
// ADD SPECIFIC FILES YOU WANT TO IGNORE HERE
$ignore_file_list = array( ".htaccess", "Thumbs.db", ".DS_Store", "index.php" );
// ADD SPECIFIC FILE EXTENSIONS YOU WANT TO IGNORE HERE, EXAMPLE: array('psd','jpg','jpeg')
$ignore_ext_list = array( );
// SORT BY
$sort_by = "name_asc"; // options: name_asc, name_desc, date_asc, date_desc
// ICON URL
$icon_url = "https://dl.dropbox.com/u/6771946/icons/icons.png";
// TOGGLE SUB FOLDERS, SET TO false IF YOU WANT OFF
$toggle_sub_folders = true;
// SET TITLE BASED ON FOLDER NAME, IF NOT SET ABOVE
if( !$title ) { $title = cleanTitle(basename(dirname(__FILE__))); }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo $title; ?></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Lato:700,400,300,300italic,700italic" rel="stylesheet" type="text/css" />
<style>
*, *:before, *:after { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; }
body { font-family: "Lato", "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; font-weight: 400; font-size: 14px; line-height: 18px; padding: 0; margin: 0; background: #f5f5f5; }
.wrap { max-width: 600px; margin: 20px auto; background: white; padding: 40px; box-shadow: 0 0 2px #ccc; }
@media only screen and (max-width: 700px) { .wrap { padding: 15px; } }
h1 { text-align: center; margin: 40px 0; font-size: 22px; font-weight: bold; color: #666; }
a { color: #399ae5; text-decoration: none; } a:hover { color: #206ba4; text-decoration: none; }
.note { padding: 0 5px 25px 0; font-size:80%; color: #666; line-height: 18px; }
.block { clear: both; min-height: 50px; border-top: solid 1px #ECE9E9; }
.block:first-child { border: none; }
.block .img { width: 50px; height: 50px; display: block; float: left; margin-right: 10px; background: transparent url(https://dl.dropbox.com/u/6771946/icons/icons.png) no-repeat 0 0; }
.block .date { margin-top: 4px; font-size: 70%; color: #666; }
.block a { display: block; padding: 10px 15px; transition: all 0.35s; }
.block a:hover { text-decoration: none; background: #efefef; }
.jpg, .jpeg, .gif, .png { background-position: -50px 0 !important; }
.pdf { background-position: -100px 0 !important; }
.txt, .rtf { background-position: -150px 0 !important; }
.xls, .xlsx { background-position: -200px 0 !important; }
.ppt, .pptx { background-position: -250px 0 !important; }
.doc, .docx { background-position: -300px 0 !important; }
.zip, .rar, .tar, .gzip { background-position: -350px 0 !important; }
.swf { background-position: -400px 0 !important; }
.fla { background-position: -450px 0 !important; }
.mp3 { background-position: -500px 0 !important; }
.wav { background-position: -550px 0 !important; }
.mp4 { background-position: -600px 0 !important; }
.mov, .aiff, .m2v, .avi, .pict, .qif { background-position: -650px 0 !important; }
.wmv, .avi, .mpg { background-position: -700px 0 !important; }
.flv, .f2v { background-position: -750px 0 !important; }
.psd { background-position: -800px 0 !important; }
.ai { background-position: -850px 0 !important; }
.html, .xhtml, .dhtml, .php, .asp, .css, .js, .inc { background-position: -900px 0 !important; }
.dir { background-position: -950px 0 !important; }
.sub { margin-left: 20px; border-left: solid 1px #ECE9E9; display: none; }
</style>
</head>
<body>
<h1><?php echo $title ?></h1>
<div class="wrap">
<?php
// FUNCTIONS TO MAKE THE MAGIC HAPPEN, BEST TO LEAVE THESE ALONE
function cleanTitle($title)
{
return ucwords( str_replace( array("-", "_"), " ", $title) );
}
function getFileExt($filename)
{
return substr( strrchr( $filename,'.' ),1 );
}
function format_size($file)
{
$bytes = filesize($file);
if ($bytes < 1024) return $bytes.'b';
elseif ($bytes < 1048576) return round($bytes / 1024, 2).'kb';
elseif ($bytes < 1073741824) return round($bytes / 1048576, 2).'mb';
elseif ($bytes < 1099511627776) return round($bytes / 1073741824, 2).'gb';
else return round($bytes / 1099511627776, 2).'tb';
}
// SHOW THE MEDIA BLOCK
function display_block( $file )
{
global $ignore_file_list, $ignore_ext_list;
$file_ext = getFileExt($file);
if( !$file_ext AND is_dir($file)) { $file_ext = "dir"; }
if(in_array($file, $ignore_file_list)) { return; }
if(in_array($file_ext, $ignore_ext_list)) { return; }
echo "<div class=\"block\">";
echo "<a href=\"$file\" class=\"$file_ext\">";
echo " <div class=\"img $file_ext\"> </div>";
echo " <div class=\"name\">\n";
echo " <div class=\"file\">" . basename($file) . "</div>\n";
echo " <div class=\"date\">Size: " . format_size($file) . "<br />Last modified: " . date("D. F jS, Y - h:ia", filemtime($file)) . "</div>\n";
echo " </div>\n";
echo " </a>\n";
echo "</div>";
}
// RECURSIVE FUNCTION TO BUILD THE BLOCKS
function build_blocks( $items, $folder )
{
global $ignore_file_list, $ignore_ext_list, $sort_by, $toggle_sub_folders;
$objects = array();
$objects['directories'] = array();
$objects['files'] = array();
foreach($items as $c => $item)
{
if( $item == ".." OR $item == ".") continue;
// IGNORE FILE
if(in_array($item, $ignore_file_list)) { continue; }
if( $folder )
{
$item = "$folder/$item";
}
$file_ext = getFileExt($item);
// IGNORE EXT
if(in_array($file_ext, $ignore_ext_list)) { continue; }
// DIRECTORIES
if( is_dir($item) )
{
$objects['directories'][] = $item;
continue;
}
// FILE DATE
$file_time = date("U", filemtime($item));
// FILES
$objects['files'][$file_time . "-" . $item] = $item;
}
foreach($objects['directories'] as $c => $file)
{
display_block( $file );
if($toggle_sub_folders)
{
$sub_items = (array) scandir( $file );
if( $sub_items )
{
echo "<div class='sub' data-folder=\"$file\">";
build_blocks( $sub_items, $file );
echo "</div>";
}
}
}
// SORT BEFORE LOOP
if( $sort_by == "date_asc" ) { ksort($objects['files']); }
elseif( $sort_by == "date_desc" ) { krsort($objects['files']); }
elseif( $sort_by == "name_asc" ) { natsort($objects['files']); }
elseif( $sort_by == "name_desc" ) { arsort($objects['files']); }
foreach($objects['files'] as $t => $file)
{
$fileExt = getFileExt($file);
if(in_array($file, $ignore_file_list)) { continue; }
if(in_array($fileExt, $ignore_ext_list)) { continue; }
display_block( $file );
}
}
// GET THE BLOCKS STARTED, FALSE TO INDICATE MAIN FOLDER
$items = scandir( dirname(__FILE__) );
build_blocks( $items, false );
?>
<?php if($toggle_sub_folders) { ?>
<script>
$(document).ready(function()
{
$("a.dir").click(function(e)
{
$('.sub[data-folder="' + $(this).attr('href') + '"]').slideToggle();
e.preventDefault();
});
});
</script>
<?php } ?>
</div>
</body>
</html>