1
|
|
|
<?php |
2
|
|
|
namespace Filesystem; |
3
|
|
|
|
4
|
|
|
class Filesystem |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Changes mode for files/directories. |
9
|
|
|
* |
10
|
|
|
* @param string $path path to the file or file |
11
|
|
|
* @param int $mode mode |
12
|
|
|
* @param bool $recursive recursive file mode change |
13
|
|
|
* @return bool |
14
|
|
|
*/ |
15
|
|
|
final public static function chmod($path, $mode, $recursive = false, $strict = true) |
16
|
|
|
{ |
17
|
|
|
$mode = self::fixFileModeFormat($mode); |
18
|
|
|
$recursive = ($recursive == false) ? '': '-R'; |
|
|
|
|
19
|
|
|
system("chmod {$recursive} {$mode} {$path} 2> /dev/null", $retval); |
20
|
|
|
|
21
|
|
|
if ($strict == false && $retval != 0) |
22
|
|
|
{ |
23
|
|
|
return false; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if ($strict && $retval != 0) |
27
|
|
|
{ |
28
|
|
|
throw new \RuntimeException(__METHOD__ . ": failed to change or update file permissions for {$path}"); |
29
|
|
|
} |
30
|
|
|
return true; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Changes group of file. |
35
|
|
|
* |
36
|
|
|
* @param string $file path to the file |
37
|
|
|
* @param int $group file group |
38
|
|
|
* @param bool $recursive recursive file mode change |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
final public static function chgrp($file, $group, $recursive = false, $strict = true) |
42
|
|
|
{ |
43
|
|
|
$recursive = ($recursive == false) ?'': '-R'; |
|
|
|
|
44
|
|
|
system("chgrp {$recursive} {$group} {$file} 2> /dev/null", $retval); |
45
|
|
|
|
46
|
|
|
if ($strict == false && $retval != 0) |
47
|
|
|
{ |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($strict && $retval != 0) |
52
|
|
|
{ |
53
|
|
|
throw new \RuntimeException(__METHOD__ . ": failed to set file '{$file}' to {$group}"); |
54
|
|
|
} |
55
|
|
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Copies file. |
60
|
|
|
* |
61
|
|
|
* @param string $srcfile path to the source file |
62
|
|
|
* @param string $destfile path to the destination file |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
final public static function copy($srcfile, $destfile) |
66
|
|
|
{ |
67
|
|
|
self::fileExists($srcfile); |
68
|
|
|
if (@copy($srcfile, $destfile) === false) |
69
|
|
|
{ |
70
|
|
|
throw new \RuntimeException(__METHOD__ . ": failed to create copy of '{$srcfile}' to '{$destfile}'"); |
71
|
|
|
} |
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Removes file. |
77
|
|
|
* |
78
|
|
|
* @param string $file file name |
79
|
|
|
* @param bool $strict strict error checking |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
final public static function delete($file, $strict = true) |
83
|
|
|
{ |
84
|
|
|
self::fileExists($file); |
85
|
|
|
if (@unlink($file) === false) |
86
|
|
|
{ |
87
|
|
|
if ($strict) |
88
|
|
|
{ |
89
|
|
|
throw new \RuntimeException(__METHOD__ . ": failed to delete {$file}"); |
90
|
|
|
} |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Removes files. |
98
|
|
|
* |
99
|
|
|
* @param array $files file names |
100
|
|
|
* @param bool $strict strict error checking |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
final public static function deleteFiles($files, $strict = true) |
104
|
|
|
{ |
105
|
|
|
foreach ($files as $file) |
106
|
|
|
{ |
107
|
|
|
self::delete($file, $strict); |
108
|
|
|
} |
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns pathnames matching a pattern (for files only & hidden files). |
114
|
|
|
* |
115
|
|
|
* @param string $directory directory |
116
|
|
|
* @param mixed $pattern pattern (does not support tilde expansion) |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
final public static function glob($directory, $pattern = null) |
120
|
|
|
{ |
121
|
|
|
self::dirExists($directory); |
122
|
|
|
|
123
|
|
|
$pattern = is_array(($pattern)) ? '{' . implode(',', $pattern) . '}' : $pattern; |
124
|
|
|
if ($pattern === null) |
125
|
|
|
{ |
126
|
|
|
return array_diff(glob($directory . '/*'), ['.', '..']); |
127
|
|
|
} |
128
|
|
|
return glob($directory . '/{,.}*' . $pattern . '*', GLOB_BRACE); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Create new directory. |
133
|
|
|
* |
134
|
|
|
* @param mixed $directory path to the directory |
135
|
|
|
* @param int $mode directory permission mode value (octal) |
136
|
|
|
* @param bool $recursive create directories as needed |
137
|
|
|
* @param bool $strict strict error checking |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
final public static function mkdir($directory, $mode = 0777, $recursive = false, $strict = true) |
141
|
|
|
{ |
142
|
|
|
if (self::dirExists($directory, false)) |
143
|
|
|
{ |
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (@mkdir($directory, self::fixFileModeFormat($mode), $recursive) === false) |
148
|
|
|
{ |
149
|
|
|
if ($strict) |
150
|
|
|
{ |
151
|
|
|
throw new \RuntimeException(__METHOD__ . ": failed to create new directory {$directory}"); |
152
|
|
|
} |
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
return true; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Create new directories. |
160
|
|
|
* |
161
|
|
|
* @param mixed $directories path to the directories |
162
|
|
|
* @param int $mode directory permission mode value (octal) |
163
|
|
|
* @param bool $recursive create directories as needed |
164
|
|
|
* @param bool $strict strict error checking |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
final public static function mkdirs($directories, $mode = 0777, $recursive = false, $strict = true) |
168
|
|
|
{ |
169
|
|
|
foreach ($directories as $directory) |
170
|
|
|
{ |
171
|
|
|
self::mkdir($directory, $mode, $recursive, $strict); |
172
|
|
|
self::chmod($directory, $mode); |
173
|
|
|
} |
174
|
|
|
return true; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Removes a directory. |
179
|
|
|
* |
180
|
|
|
* @param string $directory directory to remove |
181
|
|
|
* @param bool $strict strict error checking |
182
|
|
|
* @return mixed |
183
|
|
|
*/ |
184
|
|
|
final public static function rmdir($directory, $strict = true) |
185
|
|
|
{ |
186
|
|
|
self::dirExists($directory); |
187
|
|
|
|
188
|
|
|
// Remove any files, if found in directory |
189
|
|
|
if (count(($files = self::glob($directory))) > 0) |
190
|
|
|
{ |
191
|
|
|
self::deleteFiles($files); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if (@rmdir($directory) === false) |
195
|
|
|
{ |
196
|
|
|
if ($strict) |
197
|
|
|
{ |
198
|
|
|
throw new \RuntimeException(__METHOD__ . ": failed to delete directory '{$directory}'"); |
199
|
|
|
} |
200
|
|
|
return false; |
201
|
|
|
} |
202
|
|
|
return true; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Removes a directories. |
207
|
|
|
* |
208
|
|
|
* @param string $directories directories to remove |
209
|
|
|
* @param bool $strict strict error checking |
210
|
|
|
* @return mixed |
211
|
|
|
*/ |
212
|
|
|
final public static function rmdirs($directory, $strict = true) |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
foreach ($directories as $directory) |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
self::rmdir($directory); |
217
|
|
|
} |
218
|
|
|
return true; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Moves file to different path (rename). |
223
|
|
|
* |
224
|
|
|
* @param string $old_file path to the old file |
225
|
|
|
* @param string $new_file path to the new file |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
|
|
final public static function rename($old_file, $new_file) |
229
|
|
|
{ |
230
|
|
|
self::fileExists($old_file); |
231
|
|
|
if (!@rename($old_file, $new_file)) |
232
|
|
|
{ |
233
|
|
|
throw new \RuntimeException(__METHOD__ . ": failed to move '{$old_file}' to '{$new_file}'"); |
234
|
|
|
} |
235
|
|
|
return true; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Create temporary file. |
240
|
|
|
* |
241
|
|
|
* @param string $template temporary filename template (default=tmp-XXXXXXXXXXXXXX) |
242
|
|
|
* @param bool $resource request file pointer into temporary file |
243
|
|
|
* @return mixed |
244
|
|
|
*/ |
245
|
|
|
final public static function tmpfile($template = null, $resource = false) |
246
|
|
|
{ |
247
|
|
|
$template = ($template === null) ? "tmp.XXXXXXXXXXXXXX" : "{$template}.XXXXXXXXXXXXXX"; |
248
|
|
|
$tempfile = shell_exec("mktemp -p /tmp {$template}"); |
249
|
|
|
return ($resource == false) ? rtrim($tempfile) : fopen($tempfile, "w+"); |
|
|
|
|
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Create temporary directory. |
254
|
|
|
* |
255
|
|
|
* @param string $template temporary directory template (default=tmpXXXXXXXXXXXXXX) |
256
|
|
|
* @return mixed |
257
|
|
|
*/ |
258
|
|
|
final public static function tmpdir($template = null) |
259
|
|
|
{ |
260
|
|
|
$template = ($template === null) ? "tmpXXXXXXXXXXXXXX" : "{$template}XXXXXXXXXXXXXX"; |
261
|
|
|
$tempdir = shell_exec("mktemp -p /tmp -d {$template}"); |
262
|
|
|
return rtrim($tempdir); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Fix mode to proper formats. |
267
|
|
|
* |
268
|
|
|
* @param int $mode file mode |
269
|
|
|
* @return int |
270
|
|
|
*/ |
271
|
|
|
final private static function fixFileModeFormat($mode) |
272
|
|
|
{ |
273
|
|
|
if (preg_match('/^[0-9]{3}$/', $mode)) |
274
|
|
|
{ |
275
|
|
|
return str_pad(decoct($mode), 4, '0', STR_PAD_LEFT); |
|
|
|
|
276
|
|
|
} |
277
|
|
|
return $mode; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Checks if directory exists;. |
282
|
|
|
* |
283
|
|
|
* @param string $directory directory name |
284
|
|
|
* @param bool $strict strict error checking |
285
|
|
|
* @return bool |
286
|
|
|
*/ |
287
|
|
|
final public static function dirExists($directory, $strict = true) |
288
|
|
|
{ |
289
|
|
|
if (!file_exists($directory)) |
290
|
|
|
{ |
291
|
|
|
if ($strict) |
292
|
|
|
{ |
293
|
|
|
throw new \RuntimeException(__METHOD__ . ": directory doesn't exist: {$directory}."); |
294
|
|
|
} |
295
|
|
|
return false; |
296
|
|
|
} |
297
|
|
|
return true; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Checks if file exists;. |
302
|
|
|
* |
303
|
|
|
* @param string $file file name |
304
|
|
|
* @param bool $strict strict error checking |
305
|
|
|
* @return bool |
306
|
|
|
*/ |
307
|
|
|
final public static function fileExists($file, $strict = true) |
308
|
|
|
{ |
309
|
|
|
if (!file_exists($file)) |
310
|
|
|
{ |
311
|
|
|
if ($strict) |
312
|
|
|
{ |
313
|
|
|
throw new \RuntimeException(__METHOD__ . ": file doesn't exist: {$file}."); |
314
|
|
|
} |
315
|
|
|
return false; |
316
|
|
|
} |
317
|
|
|
return true; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
} |
321
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.