-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
145 lines (118 loc) · 3.23 KB
/
functions.php
File metadata and controls
145 lines (118 loc) · 3.23 KB
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
<?php
function getFolders($path, $protected = [])
{
$result = [];
if (!is_dir($path) || !is_readable($path))
return $result;
$folders = scandir($path);
if ($folders === false)
return $result;
foreach ($folders as $folder) {
if ($folder === "." || $folder === "..")
continue;
$fullPath = $path . "/" . $folder;
if (!in_array($folder, $protected) && is_dir($fullPath) && is_readable($fullPath)) {
$result[] = $folder;
}
}
return $result;
}
function getFiles($path)
{
$result = [];
if (!is_dir($path) || !is_readable($path))
return $result;
$files = scandir($path);
if ($files === false)
return $result;
foreach ($files as $file) {
$fullPath = $path . '/' . $file;
if (is_file($fullPath) && is_readable($fullPath)) {
$result[] = $file;
}
}
return $result;
}
function dig($path, $bomb = [])
{
static $result = [];
if (!is_dir($path) || !is_readable($path))
return $result;
$result[] = $path;
$currentFolders = getFolders($path, $bomb);
foreach ($currentFolders as $folder) {
$currentPath = $path . "/" . $folder;
$result[] = $currentPath;
dig($currentPath, $bomb);
}
return $result;
}
function collectTunnel($tunnel)
{
$result = [];
foreach ($tunnel as $folder) {
$result[$folder] = getFiles($folder);
}
return $result;
}
function getSize($path)
{
if (!file_exists($path) || !is_readable($path))
return 0;
if (is_file($path))
return filesize($path);
if (is_dir($path)) {
$totalSize = 0;
try {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)
);
foreach ($iterator as $file) {
if ($file->isReadable()) {
$totalSize += $file->getSize();
}
}
} catch (Exception $e) {
}
return $totalSize;
}
return 0;
}
function formatSize($bytes)
{
$units = ['o', 'Ko', 'Mo', 'Go', 'To'];
for ($i = 0; $bytes >= 1024 && $i < count($units) - 1; $i++) {
$bytes /= 1024;
}
return round($bytes, 2) . ' ' . $units[$i];
}
function getCreationDate($path)
{
if (!file_exists($path) || !is_readable($path))
return false;
return date("Y-m-d H:i:s", filectime($path));
}
function getLastModificationDate($path)
{
if (!file_exists($path) || !is_readable($path))
return false;
if (is_file($path))
return date("Y-m-d H:i:s", filemtime($path));
$lastMod = filemtime($path) ?: 0;
if (is_dir($path)) {
try {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)
);
foreach ($iterator as $file) {
if ($file->isReadable()) {
$fileModTime = $file->getMTime();
if ($fileModTime > $lastMod)
$lastMod = $fileModTime;
}
}
} catch (Exception $e) {
}
}
return date("Y-m-d H:i:s", $lastMod);
}