1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ffcms\Core\Helper\FileSystem; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\Helper\Type\Arr; |
6
|
|
|
use Ffcms\Core\Helper\Type\Str; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class File - helper to work with files |
10
|
|
|
* @package Ffcms\Core\Helper |
11
|
|
|
*/ |
12
|
|
|
class File |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Read file content from local storage |
17
|
|
|
* @param $path |
18
|
|
|
* @return bool|string |
19
|
|
|
*/ |
20
|
|
|
public static function read($path) |
21
|
|
|
{ |
22
|
|
|
$path = Normalize::diskFullPath($path); |
23
|
|
|
|
24
|
|
|
if (!self::exist($path)) { |
25
|
|
|
return false; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return @file_get_contents($path); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Check if $path is exist and readable in filesystem |
33
|
|
|
* @param string $path |
34
|
|
|
* @return bool |
35
|
|
|
*/ |
36
|
|
|
public static function exist($path) |
37
|
|
|
{ |
38
|
|
|
$path = Normalize::diskFullPath($path); |
39
|
|
|
return (file_exists($path) && is_readable($path) && is_file($path)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Alias for exist method |
44
|
|
|
* @param string $path |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public static function readable($path) { |
48
|
|
|
return self::exist($path); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Check is file writable |
53
|
|
|
* @param string $path |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public static function writable($path) |
57
|
|
|
{ |
58
|
|
|
$path = Normalize::diskFullPath($path); |
59
|
|
|
|
60
|
|
|
if (!self::exist($path)) { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return is_writable($path); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Check is file executable |
69
|
|
|
* @param string $path |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public static function executable($path) |
73
|
|
|
{ |
74
|
|
|
$path = Normalize::diskFullPath($path); |
75
|
|
|
|
76
|
|
|
if (!self::exist($path)) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return is_executable($path); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $path |
85
|
|
|
* @param null|string $content |
86
|
|
|
* @param null|int $flags |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
public static function write($path, $content = null, $flags = null) |
90
|
|
|
{ |
91
|
|
|
$path = Normalize::diskFullPath($path); |
92
|
|
|
|
93
|
|
|
$pathArray = explode(DIRECTORY_SEPARATOR, $path); |
94
|
|
|
array_pop($pathArray); |
95
|
|
|
$pathName = implode(DIRECTORY_SEPARATOR, $pathArray); |
96
|
|
|
|
97
|
|
|
if (Directory::exist($pathName)) { |
98
|
|
|
Directory::create($pathName); |
99
|
|
|
} |
100
|
|
|
return @file_put_contents($path, $content, $flags); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Remove file |
105
|
|
|
* @param string $path |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
public static function remove($path) |
109
|
|
|
{ |
110
|
|
|
$path = Normalize::diskFullPath($path); |
111
|
|
|
|
112
|
|
|
if (!self::exist($path)) { |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
return unlink($path); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Alternative of functions include, require, include_once and etc in 1 function |
121
|
|
|
* @param string $path |
122
|
|
|
* @param bool|false $return |
123
|
|
|
* @param bool|false $once |
124
|
|
|
* @return bool|mixed |
125
|
|
|
*/ |
126
|
|
|
public static function inc($path, $return = false, $once = false) |
127
|
|
|
{ |
128
|
|
|
$path = Normalize::diskFullPath($path); |
129
|
|
|
|
130
|
|
|
if (!self::exist($path)) { |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($return === true) { |
135
|
|
|
return $once === true ? require_once($path) : require $path; |
136
|
|
|
} else { |
137
|
|
|
($once == true) ? require_once($path) : require $path; |
|
|
|
|
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get file make time in unix timestamp |
143
|
|
|
* @param string $path |
144
|
|
|
* @return int |
145
|
|
|
*/ |
146
|
|
|
public static function mTime($path) |
147
|
|
|
{ |
148
|
|
|
$path = Normalize::diskFullPath($path); |
149
|
|
|
if (!self::exist($path)) { |
150
|
|
|
return 0; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return filemtime($path); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Recursive scan directory, based on $path and allowed extensions $ext or without it |
158
|
|
|
* @param string $path |
159
|
|
|
* @param array $ext |
160
|
|
|
* @param bool $returnRelative |
161
|
|
|
* @param $files |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public static function listFiles($path, array $ext = null, $returnRelative = false, &$files = []) |
165
|
|
|
{ |
166
|
|
|
$path = Normalize::diskFullPath($path); |
167
|
|
|
|
168
|
|
|
$dir = opendir($path . '/.'); |
169
|
|
|
while($item = readdir($dir)) { |
170
|
|
|
if (is_file($sub = $path . '/' . $item)) { |
171
|
|
|
$item_ext = Str::lastIn($item, '.'); |
172
|
|
|
if ($ext === null || Arr::in($item_ext, $ext)) { |
173
|
|
|
if ($returnRelative) { |
174
|
|
|
$files[] = $item; |
175
|
|
|
} else { |
176
|
|
|
$files[] = $path . DIRECTORY_SEPARATOR . $item; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} else { |
180
|
|
|
if ($item !== '.' && $item !== '..') { |
181
|
|
|
self::listFiles($sub, $ext, $returnRelative, $files); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $files; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
} |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.