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