|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ffcms\Core\Helper\FileSystem; |
|
4
|
|
|
|
|
5
|
|
|
use FilesystemIterator; |
|
6
|
|
|
use RecursiveDirectoryIterator; |
|
7
|
|
|
use RecursiveIteratorIterator; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Directory. Provide methods to work with directories in current filesystem. |
|
11
|
|
|
* @package Ffcms\Core\Helper\FileSystem |
|
12
|
|
|
*/ |
|
13
|
|
|
class Directory |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Check if directory is exist and readable. Alias for File::exist() |
|
17
|
|
|
* @param string $path |
|
18
|
|
|
* @return bool |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function exist($path) |
|
21
|
|
|
{ |
|
22
|
|
|
$path = Normalize::diskFullPath($path); |
|
23
|
|
|
|
|
24
|
|
|
return (file_exists($path) && is_readable($path) && is_dir($path)); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Check if directory is writable |
|
29
|
|
|
* @param string $path |
|
30
|
|
|
* @return bool |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function writable($path) |
|
33
|
|
|
{ |
|
34
|
|
|
$path = Normalize::diskFullPath($path); |
|
35
|
|
|
|
|
36
|
|
|
if (!self::exist($path)) { |
|
37
|
|
|
return false; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return (is_dir($path) && is_writable($path)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create directory with recursive support. |
|
45
|
|
|
* @param string $path |
|
46
|
|
|
* @param int $chmod |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function create($path, $chmod = 0755) |
|
50
|
|
|
{ |
|
51
|
|
|
$path = Normalize::diskFullPath($path); |
|
52
|
|
|
|
|
53
|
|
|
if (self::exist($path)) { |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return @mkdir($path, $chmod, true); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Remove directory recursive. |
|
62
|
|
|
* @param string $path |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function remove($path) |
|
66
|
|
|
{ |
|
67
|
|
|
$path = Normalize::diskFullPath($path); |
|
68
|
|
|
|
|
69
|
|
|
if (!self::exist($path)) { |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $dir) { |
|
74
|
|
|
$dir->isFile() ? @unlink($dir->getPathname()) : @rmdir($dir->getPathname()); |
|
75
|
|
|
} |
|
76
|
|
|
return @rmdir($path); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Scan files in directory and return full or relative path |
|
81
|
|
|
* @param string $path |
|
82
|
|
|
* @param int $mod |
|
83
|
|
|
* @param bool|false $returnRelative |
|
84
|
|
|
* @return array|bool |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function scan($path, $mod = GLOB_ONLYDIR, $returnRelative = false) |
|
87
|
|
|
{ |
|
88
|
|
|
$path = Normalize::diskFullPath($path); |
|
89
|
|
|
|
|
90
|
|
|
if (!self::exist($path)) { |
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$pattern = rtrim($path, '/') . '/*'; |
|
95
|
|
|
$entry = glob($pattern, $mod); |
|
96
|
|
|
|
|
97
|
|
|
if ($returnRelative === true) { |
|
98
|
|
|
foreach ($entry as $key => $value) { |
|
99
|
|
|
$entry[$key] = trim(str_replace($path, null, $value), '/'); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $entry; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Rename directory based only on new name for last element. Example: rename('/var/www/html', 'php') => /var/www/php |
|
108
|
|
|
* @param string $path |
|
109
|
|
|
* @param string $newDirName |
|
110
|
|
|
* @return bool |
|
111
|
|
|
*/ |
|
112
|
|
|
public static function rename($path, $newDirName) |
|
113
|
|
|
{ |
|
114
|
|
|
$path = Normalize::diskFullPath($path); |
|
115
|
|
|
|
|
116
|
|
|
if (!self::exist($path) || !is_writable($path)) { |
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$path = rtrim($path, DIRECTORY_SEPARATOR); |
|
121
|
|
|
$separatedPath = explode(DIRECTORY_SEPARATOR, $path); |
|
122
|
|
|
array_pop($separatedPath); |
|
123
|
|
|
$clearPath = implode(DIRECTORY_SEPARATOR, $separatedPath); |
|
124
|
|
|
|
|
125
|
|
|
@rename($path, $clearPath . DIRECTORY_SEPARATOR . $newDirName); |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
return true; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Change chmod recursive inside defined folder |
|
132
|
|
|
* @param string $path |
|
133
|
|
|
* @param int $mod |
|
134
|
|
|
*/ |
|
135
|
|
|
public static function recursiveChmod($path, $mod = 0777) |
|
136
|
|
|
{ |
|
137
|
|
|
$path = Normalize::diskFullPath($path); |
|
138
|
|
|
if (!self::exist($path)) { |
|
139
|
|
|
return; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$dir = new \DirectoryIterator($path); |
|
143
|
|
|
foreach ($dir as $item) { |
|
144
|
|
|
// change chmod for folders and files |
|
145
|
|
|
if ($item->isDir() || $item->isFile()) { |
|
146
|
|
|
chmod($item->getPathname(), 0777); |
|
147
|
|
|
} |
|
148
|
|
|
// try to recursive chmod folders |
|
149
|
|
|
if ($item->isDir() && !$item->isDot()) { |
|
150
|
|
|
self::recursiveChmod($item->getPathname(), $mod); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Get directory total size (in bytes) |
|
157
|
|
|
* @param string $path |
|
158
|
|
|
* @return int |
|
159
|
|
|
*/ |
|
160
|
|
|
public static function size($path) |
|
161
|
|
|
{ |
|
162
|
|
|
$path = Normalize::diskFullPath($path); |
|
163
|
|
|
if (!self::exist($path)) { |
|
164
|
|
|
return 0; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$size = 0; |
|
168
|
|
|
try { |
|
169
|
|
|
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $file) { |
|
170
|
|
|
if ($file->getFileName() !== '..') { |
|
171
|
|
|
$size += $file->getSize(); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
} catch (\Exception $e) { |
|
175
|
|
|
return 0; |
|
176
|
|
|
} |
|
177
|
|
|
return $size; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: