-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathphp-file-manager.php
622 lines (577 loc) · 24.9 KB
/
php-file-manager.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
<?php
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$action = filter_input(INPUT_GET, "action");
if (!isset($action) || is_null($action) || $action == "") {
$action = "list";
$folder = filter_input(INPUT_GET, "folder");
if (!isset($folder) || is_null($folder) || $folder == "") {
$folder = ".";
}
}
switch ($action) {
case "expandFolder":
$path = filter_input(INPUT_GET, "path");
$level = filter_input(INPUT_GET, "level");
echo expandFolder($path, $level+1);
exit();
break;
}
//action and folder variables are set. Now print the header stuff
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Language" content="cs">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<title>PHP File manager</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</head>
<body>
<script language="Javascript">
function showPrmDlg(src){
var path = $(src).parents("TR").find("TD#name").html();
$("DIV#changePermissionsModal DIV.modal-header H4#heading SPAN").html(path);
$("DIV#changePermissionsModal").modal("show");
}
function showUnpDlg(src){
var path = $(src).parents("TR").find("TD#name").html();
$("DIV#unpackModal DIV.modal-header H4#heading SPAN").html(path);
$("DIV#unpackModal").modal("show");
}
function showPckDlg(src){
var path = $(src).parents("TR").find("TD#name").html();
$("DIV#packModal DIV.modal-header H4#heading SPAN").html(path);
$("DIV#packModal").modal("show");
}
function showMkdDlg(src){
var path = $(src).parents("TR").find("TD#name").html();
$("DIV#mkDirModal DIV.modal-header H4#heading SPAN").html(path);
$("DIV#mkDirModal").modal("show");
}
function showDltDlg(src){
var tr = $(src).parents("TR");
var parent = tr.attr("data-parent");
var path = "";
if(typeof parent != "undefined"){
path = parent + "/" + tr.find("TD#name").html();
} else {
path = tr.find("TD#name").html();
}
$("DIV#deleteModal DIV.modal-header H4#heading SPAN").html(path);
$("DIV#deleteModal").modal("show");
}
function deleteDir(){
var path = $("DIV#deleteModal DIV.modal-header H4#heading SPAN").html();
$.ajax({
url: "php-file-manager.php?action=deleteDir&path="+path,
}).done(function( data ) {
$("DIV#showLog PRE").html(data);
$("DIV#showLog").modal("show");
});
}
function makeDir(){
var path = $("DIV#mkDirModal DIV.modal-header H4#heading SPAN").html();
var name = $("DIV#mkDirModal TR#dirPath INPUT[type='text']").val();
$.ajax({
url: "php-file-manager.php?action=makeDir&path="+path+"&name="+name,
}).done(function( data ) {
location.reload();
});
}
function folderPermCheck(){
if($("DIV#changePermissionsModal TR#folderPerm INPUT[type='checkbox']").prop('checked') == true){
$("DIV#changePermissionsModal TR#folderPerm INPUT[type='text']").prop('disabled', false);
} else {
$("DIV#changePermissionsModal TR#folderPerm INPUT[type='text']").prop('disabled', true);
}
}
function filePermCheck(){
if($("DIV#changePermissionsModal TR#filePerm INPUT[type='checkbox']").prop('checked') == true){
$("DIV#changePermissionsModal TR#filePerm INPUT[type='text']").prop('disabled', false);
} else {
$("DIV#changePermissionsModal TR#filePerm INPUT[type='text']").prop('disabled', true);
}
}
function changePerm(){
var path = $("DIV#changePermissionsModal DIV.modal-header H4#heading SPAN").html();
var recursively = $("DIV#changePermissionsModal TR#recursively INPUT[type='checkbox']").prop('checked');
var filesChange = $("DIV#changePermissionsModal TR#filePerm INPUT[type='checkbox']").prop('checked');
var foldersChange = $("DIV#changePermissionsModal TR#folderPerm INPUT[type='checkbox']").prop('checked');
var filesPerm = $("DIV#changePermissionsModal TR#filePerm INPUT[type='text']").val();
var foldersPerm = $("DIV#changePermissionsModal TR#folderPerm INPUT[type='text']").val();
$.ajax({
url: "php-file-manager.php?path="+path+"&action=changeChmod&recursively="+recursively+"&filesChange="+filesChange+"&foldersChange="+foldersChange+"&filesPerm="+filesPerm+"&foldersPerm="+foldersPerm,
}).done(function( data ) {
$("DIV#showLog PRE").html(data);
$("DIV#showLog").modal("show");
});
}
function unPack(){
var file = $("DIV#unpackModal DIV.modal-header H4#heading SPAN").html();
var path = $("DIV#unpackModal TR#unpackPath INPUT[type='text']").val();
$.ajax({
url: "php-file-manager.php?action=unpack&file="+file+"&path="+path,
}).done(function( data ) {
$("DIV#showLog PRE").html(data);
$("DIV#showLog").modal("show");
});
}
function pack(){
var path = $("DIV#packModal DIV.modal-header H4#heading SPAN").html();
var file = $("DIV#packModal TR#filename INPUT[type='text']").val();
$.ajax({
url: "php-file-manager.php?action=pack&file="+file+"&path="+path,
}).done(function( data ) {
location.reload();
});
}
function expandFolder(src){
var tr = $(src).parents("TR");
var level = tr.attr("data-level");
var parent = tr.attr("data-parent");
var path = "";
if(typeof parent != "undefined"){
path = parent + "/" + tr.find("TD#name").html();
} else {
path = tr.find("TD#name").html();
}
$.ajax({
url: "php-file-manager.php?action=expandFolder&path="+path+"&level="+level,
}).done(function( data ) {
var arr = tr.find("TD#type SPAN");
arr.removeClass("glyphicon-folder-close");
arr.attr("onclick","collapseFolder(this)");
arr.addClass("glyphicon-folder-open");
tr.after(data);
});
}
function collapseFolder(src){
var tr = $(src).parents("TR");
var level = tr.attr("data-level");
tr.nextAll("TR").each(function(){
var lvl = $(this).attr("data-level");
if(lvl>level){
$(this).remove();
} else {
return;
}
});
$(src).removeClass("glyphicon-folder-open");
$(src).addClass("glyphicon-folder-close");
$(src).attr("onclick","expandFolder(this)");
}
</script>
<?php
switch ($action) {
case "list":
printFolderTable($folder);
break;
case "deleteDir":
$path = filter_input(INPUT_GET, "path");
delete_directory($path);
exit();
break;
case "makeDir":
$path = filter_input(INPUT_GET, "path")==""?"":filter_input(INPUT_GET, "path") . "/";
$name = filter_input(INPUT_GET, "name");
mkdir(__DIR__ . "/" . $path . $name, 0755);
exit();
break;
case "unpack":
$path = filter_input(INPUT_GET, "path");
$file = filter_input(INPUT_GET, "file");
unpackFile($file, $path);
exit();
break;
case "pack":
$path = filter_input(INPUT_GET, "path");
$file = filter_input(INPUT_GET, "file") . ".zip";
packDir($path, $file);
exit();
break;
case "changeChmod":
$path = filter_input(INPUT_GET, "path");
$recursively = filter_input(INPUT_GET, "recursively") == "true";
$filesChange = filter_input(INPUT_GET, "filesChange") == "true";
$foldersChange = filter_input(INPUT_GET, "foldersChange") == "true";
$filesPerm = (filter_input(INPUT_GET, "filesPerm"));
$foldersPerm = (filter_input(INPUT_GET, "foldersPerm"));
recursiveChmod($path, $recursively, $filesChange, $foldersChange, $filesPerm, $foldersPerm);
exit();
break;
}
?>
<!-- Modal - change permissions -->
<div class="modal fade" id="changePermissionsModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="heading">Change permissions of <span></span></h4>
</div>
<div class="modal-body">
<table>
<tr id="recursively"><td>Including files and subfolders</td><td style="padding-left: 15px"><input type="checkbox" checked></td></tr>
<tr id="folderPerm"><td>Folder permissions:</td><td style="padding-left: 15px"><input type="checkbox" checked onclick="folderPermCheck()"><input type="text" value="0755" style="margin-left: 5px"></td></tr>
<tr id="filePerm"><td>File permissions:</td><td style="padding-left: 15px"><input type="checkbox" checked onclick="filePermCheck()"><input type="text" value="0644" style="margin-left: 5px"></td></tr>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" onclick="changePerm()">Change permissions</button>
</div>
</div>
</div>
</div>
<!-- Modal - unpack -->
<div class="modal fade" id="unpackModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="heading">Unpack file <span></span></h4>
</div>
<div class="modal-body">
<table>
<tr id="unpackPath"><td>Path where to unpack:</td><td style="padding-left: 15px"><input type="text" value="." style="margin-left: 5px"></td></tr>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" onclick="unPack()">Unpack</button>
</div>
</div>
</div>
</div>
<!-- Modal - pack -->
<div class="modal fade" id="packModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="heading">Pack <span></span></h4>
</div>
<div class="modal-body">
<table>
<tr id="filename"><td>File name:</td><td style="padding-left: 15px"><input type="text" value="archive" style="margin-left: 5px">.zip</td></tr>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" onclick="pack()">Pack</button>
</div>
</div>
</div>
</div>
<!-- Modal - Make Directory -->
<div class="modal fade" id="mkDirModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="heading">Create directory in <span></span></h4>
</div>
<div class="modal-body">
<table>
<tr id="dirPath"><td>Name:</td><td style="padding-left: 15px"><input type="text" value="New" style="margin-left: 5px"></td></tr>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" onclick="makeDir()">Create</button>
</div>
</div>
</div>
</div>
<!-- Modal - delete -->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="heading">Delete: <span></span></h4>
</div>
<div class="modal-footer">
<button type="button" class="btn" onclick="deleteDir()">Yes</button>
<button type="button" class="btn" data-dismiss="modal">No</button>
</div>
</div>
</div>
</div>
<!-- Log -->
<div class="modal fade" id="showLog" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<pre>
</pre>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" onclick="location.reload()">Close & Refresh</button>
</div>
</div>
</div>
</div>
</body>
</html>
<?php
//functions
function eraseFile($file) {
$handle = fopen($file, "w+");
if ($handle == false)
return false;
if (!fclose($handle))
return false;
return true;
}
function unpackFile($file, $path){
echo "Unpacking file $file to directory $path";
if(!is_dir($path)){
echo "$path does not exist or is not a directory";
return false;
}
if(!is_file($file)){
echo "$file does not exist";
return false;
}
$ext = pathinfo($file, PATHINFO_EXTENSION);
if (strtoupper($ext) == "ZIP") {
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
$zip->extractTo($path);
$zip->close();
echo "File $file unzipped succesfully to path $path";
} else {
echo "Unpacking failed";
}
}
}
function packDir($source, $destination){
$txt = "";
if (!extension_loaded('zip') || ($source != "" && !file_exists($source))) return false;
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) return false;
if($source == ""){ // packing root dir
$sourcePath = str_replace('\\', '/', realpath(__DIR__));
} else {
$sourcePath = str_replace('\\', '/', realpath($source));
}
if (is_dir($sourcePath) === true){
if($source != ""){
$zip->addEmptyDir(str_replace($sourcePath . '/', '', $source . '/'));
}
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sourcePath), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file){
$file = str_replace('\\', '/', $file);
// Ignore "." and ".." folders
if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
continue;
$file = realpath($file);
if (is_dir($file) === true){
$zip->addEmptyDir(str_replace($sourcePath . '/', '', $source . '/' . $file . '/'));
} else if (is_file($file) === true){
$zip->addFile($file,str_replace($sourcePath . '/', '', $source . '/' . $file));
}
}
} else if (is_file($sourcePath) === true){
$zip->addFile($file,basename($sourcePath));
}
return $zip->close();
}
function delete_directory($path) {
echo "Deleting Contents Of: $path<br /><br />";
if(is_file($path)){
unlink($path);
echo "Deleted File: $path<br />";
return true;
}
$files = array_diff(scandir($path), array('.', '..'));
foreach ($files as $file) {
if(is_dir("$path/$file")){
delete_directory("$path/$file");
} else {
if(unlink("$path/$file"))
echo "Deleted File: $path/$file<br />";
}
}
if (rmdir($path))
echo "Deleted directory: $path<br />";
return true;
}
function recurse_copy($src, $dst) {
$dir = opendir($src);
@mkdir($dst);
while (false !== ( $file = readdir($dir))) {
if (( $file != '.' ) && ( $file != '..' )) {
if (is_dir($src . '/' . $file)) {
recurse_copy($src . '/' . $file, $dst . '/' . $file);
} else {
echo "Copying $src/$file -> $dst/$file ... ";
if(copy($src . '/' . $file, $dst . '/' . $file)){
echo "Done.\n";
} else {
return false;
}
}
}
}
closedir($dir);
return true;
}
function recursiveChmod($path, $recursively, $filesChange, $foldersChange, $filesPerm, $foldersPerm) {
echo "Changing $path with folder perms: $foldersPerm and file perms: $filesPerm\n";
if (!file_exists($path)) {
echo "File $path does not exists!";
return(false);
}
if (is_file($path)) {
if ($filesChange) {
chmod($path, octdec($filesPerm));
echo "Changed file permissions to ".($filesPerm)." on file $path.\n";
}
} elseif (is_dir($path)) {
if ($recursively) {
$foldersAndFiles = scandir($path);
$entries = array_slice($foldersAndFiles, 2);
foreach ($entries as $entry) {
recursiveChmod($path . "/" . $entry, $recursively, $filesChange, $foldersChange, $filesPerm, $foldersPerm);
}
}
// When we are done with the contents of the directory, we chmod the directory itself
if ($foldersChange) {
chmod($path, octdec($foldersPerm));
echo "Changed directory permissions to ".($foldersPerm)." on directory $path.\n";
}
}
return(true);
}
function printFolderTable($folder){
echo "<h3>List directory " . realpath($folder) . "</h3>";
$newDir = "<span class=\"glyphicon glyphicon-plus\" aria-hidden=\"true\" style=\"cursor:pointer; top: 0px\" title=\"Create new directory...\"></span>";
$packRoot = "<span class=\"glyphicon glyphicon-compressed\" aria-hidden=\"true\" style=\"cursor:pointer; top: 0px\" title=\"Pack root directory to ZIP...\"></span>";
echo "<button class=\"btn btn-default\" style=\"margin: 0% 0% 0% 5%;\" onclick=\"showMkdDlg(this)\">$newDir</button>";
echo "<button class=\"btn btn-default\" style=\"margin-left: 5px;\" onclick=\"showPckDlg(this)\">$packRoot</button>";
echo "<table class=\"table table-bordered table-hover table-striped\" style=\"margin: 5px 5%; width: 90%\">";
$contents = scandir($folder);
echo "<tr>"
. "<th>Type</th>"
. "<th>Name</th>"
. "<th>Size [b]</th>"
. "<th>Files</th>"
. "<th>Permissions</th>"
. "<th>Actions</th>"
. "</tr>";
foreach ($contents as $line) {
if($line == "." || $line == ".." ) continue;
$dir = is_dir($line);
$icon = "glyphicon glyphicon-file";
$cursor = "cursor: auto;";
$title = "";
$click = "";
$iconColor = "";
if($dir){
$icon = "glyphicon glyphicon-folder-close";
$cursor = "cursor: pointer;";
$title = "Expand";
$click = "expandFolder(this)";
$iconColor = "color: #F0AD4E";
}
$perm = substr(sprintf('%o', fileperms($line)), -4);
$data = dirsize($line);
$size = $data["size"];
$count = $data["count"];
if($size > 1048576){
$size = round($size/1048576,2) . " MB";
} elseif ($size > 1024){
$size = round($size/1024,2) . " KB";
} else {
$size = $size . " B";
}
$changePerm = "<span class=\"glyphicon glyphicon-pencil\" aria-hidden=\"true\" style=\"cursor:pointer; margin: 0px 2px\" onclick=\"showPrmDlg(this)\" title=\"Change permissions...\"></span>";
$deleteIcon = "<span class=\"glyphicon glyphicon-remove\" aria-hidden=\"true\" style=\"cursor:pointer; margin: 0px 2px\" onclick=\"showDltDlg(this)\" title=\"Delete...\"></span>";
$ext = pathinfo($line, PATHINFO_EXTENSION);
$packIcon = "<span class=\"glyphicon glyphicon glyphicon-compressed\" aria-hidden=\"true\" style=\"cursor:pointer; margin: 0px 2px\" onclick=\"showPckDlg(this)\" title=\"Pack to zip...\"></span>";
$unpackIcon = strtoupper($ext) == "ZIP"?"<span class=\"glyphicon glyphicon-open-file\" aria-hidden=\"true\" style=\"cursor:pointer; margin: 0px 2px\" onclick=\"showUnpDlg(this)\" title=\"Unpack...\"></span>":"";
echo "<tr data-level=\"0\" >"
. "<td id=\"type\" style=\"width: 70px\"><span class=\"$icon\" style=\"$cursor $iconColor\" title=\"$title\" onclick=\"$click\" aria-hidden=\"true\"></span></td>"
. "<td id=\"name\">$line</td>"
. "<td id=\"size\" style=\"width: 120px\">$size</td>"
. "<td id=\"fileCount\" style=\"width: 120px\">$count</td>"
. "<td id=\"perm\" style=\"width: 120px\">$perm</td>"
. "<td style=\"width: 120px\">$changePerm $packIcon $unpackIcon $deleteIcon</td>"
. "</tr>";
}
echo "</table>";
}
function expandFolder($path, $level) {
$contents = scandir($path);
$margin = "margin-left: " . ($level*10) . "px;";
foreach ($contents as $line) {
if ($line == "." || $line == "..")
continue;
$dir = is_dir($path . "/" . $line);
$icon = "glyphicon glyphicon-file";
$cursor = "cursor: auto;";
$title = "";
$click = "";
$iconColor = "";
if($dir){
$icon = "glyphicon glyphicon-folder-close";
$cursor = "cursor: pointer;";
$title = "Expand";
$click = "expandFolder(this)";
$iconColor = "color: #F0AD4E";
}
$perm = substr(sprintf('%o', fileperms($path . "/" . $line)), -4);
$data = dirsize($path . "/" . $line);
$size = $data["size"];
$count = $data["count"];
if($size > 1048576){
$size = round($size/1048576,2) . " MB";
} elseif ($size > 1024){
$size = round($size/1024,2) . " KB";
} else {
$size = $size . " B";
}
$changePerm = "<span class=\"glyphicon glyphicon-pencil\" aria-hidden=\"true\" style=\"cursor:pointer; margin: 0px 5px\" onclick=\"showPrmDlg(this)\" title=\"Change permissions...\"></span>";
$deleteIcon = "<span class=\"glyphicon glyphicon-remove\" aria-hidden=\"true\" style=\"cursor:pointer; margin: 0px 5px\" onclick=\"showDltDlg(this)\" title=\"Delete...\"></span>";
$unpackIcon = "<span class=\"glyphicon glyphicon-open-file\" aria-hidden=\"true\" style=\"cursor:pointer; margin: 0px 5px\" onclick=\"showUnpDlg(this)\" title=\"Unpack...\"></span>";
echo "<tr data-level=\"$level\" data-parent=\"$path\">"
. "<td id=\"type\" style=\"width: 70px\"><span class=\"$icon\" style=\"$cursor $margin $iconColor\" title=\"$title\" onclick=\"$click\" aria-hidden=\"true\"></span></td>"
. "<td id=\"name\">$line</td>"
. "<td id=\"size\">$size</td>"
. "<td id=\"fileCount\" style=\"width: 120px\">$count</td>"
. "<td id=\"perm\">$perm</td>"
. "<td>$changePerm $deleteIcon</td>"
. "</tr>";
}
}
function dirsize($dir) {
if(is_file($dir)) return array("size" => filesize ($dir), "count"=>1);
$total_size = 0;
$files = scandir($dir);
$total_count = count($files) - 2; //do not count . and ..
foreach ($files as $t) {
if (is_dir(rtrim($dir, '/') . '/' . $t)) {
if ($t <> "." && $t <> "..") {
$data = dirsize(rtrim($dir, '/') . '/' . $t);
$size = $data["size"];
$count = $data["count"];
$total_size += $size;
$total_count += $count;
}
} else {
$size = filesize(rtrim($dir, '/') . '/' . $t);
$total_size += $size;
}
}
return array("size" => $total_size, "count"=>$total_count);
}