-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
89 lines (77 loc) · 2.54 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
<?php
echo "<pre>";
print_r(getCatalogInfo());
function getCatalogInfo($catName = false, $isJson = false)
{
if (!$catName) {
$catName = __DIR__;
}
$filesArr = array();
$totalSize = $totalFiles = $dublicat = 0;
$dublicatArr = array();
if ($catalog = opendir($catName)) {
while (($files = readdir($catalog)) !== false) {
if ($files !== '.' && $files !== '..') {
$dirName = $catName . '/' . $files;
if (is_dir($dirName)) {
$inArr = getFilesSize($dirName);
$filesArr['in'][$files] = $inArr;
}
}
}
}
if (isset($filesArr['in'])) {
$filesArr = sortArr($filesArr['in']);
}
return $isJson ? json_encode($filesArr) : $filesArr;
}
function getFilesSize($catName)
{
$totalSize = $totalFiles = $dublicat = 0;
$dublicatArr = $filesArr = $inArr = array();
$filesArr['totalSize'] = 0;
if ($catalog = opendir($catName)) {
while (($files = readdir($catalog)) !== false) {
if ($files !== '.' && $files !== '..') {
$dirName = $catName . '/' . $files;
if (file_exists($dirName) && !is_dir($dirName)) {
$fsize = filesize($dirName);
$totalFiles++;
$totalSize += $fsize;
$hash = md5(file_get_contents($dirName));
$dublicatArr[$hash] = empty($dublicatArr[$hash]) ? 1 : ++$dublicatArr[$hash];
if (isset($dublicatArr[$hash]) && $dublicatArr[$hash] > 1) {
$dublicat++;
}
} else {
$inArr = getFilesSize($dirName);
$filesArr['in'][$files] = $inArr;
$filesArr['in'][$files]['totalSize'] = $inArr['totalSize'];
$filesArr['totalSize'] += $inArr['totalSize'];
}
}
}
if (isset($filesArr['in'])) {
$filesArr['in'] = sortArr($filesArr['in']);
}
$filesArr['duplicate'] = $dublicat;
$filesArr['totalSize'] += $totalSize;
$filesArr['numberFiles'] = $totalFiles;
}
return $filesArr;
}
function sortArr($arr)
{
if (count($arr) == 0) {
return ($arr);
}
$midArr = $midArr = $newArr = array();
foreach ($arr as $name => $data) {
$midArr[$name] = $data['totalSize'];
}
asort($midArr);
foreach ($midArr as $name => $size) {
$newArr[$name] = $arr[$name];
}
return $newArr;
}