1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This file is part of the O2System Framework package.
|
4
|
|
|
*
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
6
|
|
|
* file that was distributed with this source code.
|
7
|
|
|
*
|
8
|
|
|
* @author Steeve Andrian Salim
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------
|
13
|
|
|
|
14
|
|
|
namespace O2System\Spl\Info;
|
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------
|
17
|
|
|
|
18
|
|
|
/**
|
19
|
|
|
* Class SplDirectoryInfo
|
20
|
|
|
*
|
21
|
|
|
* @package O2System\Spl\Info
|
22
|
|
|
*/
|
23
|
|
|
class SplDirectoryInfo
|
24
|
|
|
{
|
25
|
|
|
/**
|
26
|
|
|
* Directory Path Name
|
27
|
|
|
*
|
28
|
|
|
* @var string
|
29
|
|
|
*/
|
30
|
|
|
private $pathName;
|
31
|
|
|
|
32
|
|
|
/**
|
33
|
|
|
* Directory Name
|
34
|
|
|
*
|
35
|
|
|
* @var string
|
36
|
|
|
*/
|
37
|
|
|
private $dirName;
|
38
|
|
|
|
39
|
|
|
// ------------------------------------------------------------------------
|
40
|
|
|
|
41
|
|
|
/**
|
42
|
|
|
* SplDirectoryInfo::__construct
|
43
|
|
|
*
|
44
|
|
|
* @param string $dir Directory Path
|
45
|
|
|
*/
|
46
|
|
|
public function __construct($dir)
|
47
|
|
|
{
|
48
|
|
|
$this->pathName = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, realpath($dir)) . DIRECTORY_SEPARATOR;
|
49
|
|
|
$this->dirName = pathinfo($this->pathName, PATHINFO_FILENAME);
|
50
|
|
|
}
|
51
|
|
|
|
52
|
|
|
// ------------------------------------------------------------------------
|
53
|
|
|
|
54
|
|
|
/**
|
55
|
|
|
* SplDirectoryInfo::isExists
|
56
|
|
|
*
|
57
|
|
|
* Determine if the directory is exists
|
58
|
|
|
*
|
59
|
|
|
* @return bool
|
60
|
|
|
*/
|
61
|
|
|
public function isExists()
|
62
|
|
|
{
|
63
|
|
|
return (bool)is_dir($this->pathName);
|
64
|
|
|
}
|
65
|
|
|
|
66
|
|
|
// ------------------------------------------------------------------------
|
67
|
|
|
|
68
|
|
|
/**
|
69
|
|
|
* SplDirectoryInfo::getParentPath
|
70
|
|
|
*
|
71
|
|
|
* Gets the directory parent without trailing directory slash.
|
72
|
|
|
*
|
73
|
|
|
* @return string
|
74
|
|
|
*/
|
75
|
|
|
public function getParentPath()
|
76
|
|
|
{
|
77
|
|
|
return dirname($this->pathName);
|
78
|
|
|
}
|
79
|
|
|
|
80
|
|
|
// ------------------------------------------------------------------------
|
81
|
|
|
|
82
|
|
|
/**
|
83
|
|
|
* SplDirectoryInfo::getParentRealPath
|
84
|
|
|
*
|
85
|
|
|
* Gets the directory real path
|
86
|
|
|
*
|
87
|
|
|
* @return string
|
88
|
|
|
*/
|
89
|
|
|
public function getParentRealPath()
|
90
|
|
|
{
|
91
|
|
|
return dirname($this->pathName) . DIRECTORY_SEPARATOR;
|
92
|
|
|
}
|
93
|
|
|
|
94
|
|
|
// ------------------------------------------------------------------------
|
95
|
|
|
|
96
|
|
|
/**
|
97
|
|
|
* SplDirectoryInfo::getParentRelativePath
|
98
|
|
|
*
|
99
|
|
|
* Gets the directory relative path
|
100
|
|
|
*
|
101
|
|
|
* @return string
|
102
|
|
|
*/
|
103
|
|
|
public function getParentRelativePath()
|
104
|
|
|
{
|
105
|
|
|
$scriptFilename = str_replace(['/', '\\'], '/', dirname($_SERVER[ 'SCRIPT_FILENAME' ]));
|
106
|
|
|
$relativePath = str_replace(['/', '\\'], '/', dirname($this->pathName)) . '/';
|
107
|
|
|
|
108
|
|
|
if (strpos($scriptFilename, 'public')) {
|
109
|
|
|
return str_replace(dirname($scriptFilename), '..', $relativePath);
|
110
|
|
|
}
|
111
|
|
|
|
112
|
|
|
return str_replace($scriptFilename, '', $relativePath);
|
113
|
|
|
}
|
114
|
|
|
|
115
|
|
|
// ------------------------------------------------------------------------
|
116
|
|
|
|
117
|
|
|
/**
|
118
|
|
|
* SplDirectoryInfo::getPathInfo
|
119
|
|
|
*
|
120
|
|
|
* Gets an SplDirectoryInfo object for the parent of the current file.
|
121
|
|
|
*
|
122
|
|
|
* @param string|null $className Name of an SplDirectoryInfo derived class to use.
|
123
|
|
|
*
|
124
|
|
|
* @return \O2System\Spl\Info\SplDirectoryInfo
|
125
|
|
|
*/
|
126
|
|
|
public function getPathInfo($className = null)
|
127
|
|
|
{
|
128
|
|
|
return isset($className) ? new $className(pathinfo($this->pathName)) : $this;
|
129
|
|
|
}
|
130
|
|
|
|
131
|
|
|
// ------------------------------------------------------------------------
|
132
|
|
|
|
133
|
|
|
/**
|
134
|
|
|
* SplDirectoryInfo::getDirName
|
135
|
|
|
*
|
136
|
|
|
* Gets the directory name without any path information.
|
137
|
|
|
*
|
138
|
|
|
* @return string
|
139
|
|
|
*/
|
140
|
|
|
public function getDirName()
|
141
|
|
|
{
|
142
|
|
|
return $this->dirName;
|
143
|
|
|
}
|
144
|
|
|
|
145
|
|
|
// ------------------------------------------------------------------------
|
146
|
|
|
|
147
|
|
|
/**
|
148
|
|
|
* SplDirectoryInfo::getPathName
|
149
|
|
|
*
|
150
|
|
|
* Gets the path to the directory without trailing directory slash.
|
151
|
|
|
*
|
152
|
|
|
* @return null|string
|
153
|
|
|
*/
|
154
|
|
|
public function getPathName()
|
155
|
|
|
{
|
156
|
|
|
return isset($this->pathName) ? $this->getPath() : null;
|
157
|
|
|
}
|
158
|
|
|
|
159
|
|
|
// ------------------------------------------------------------------------
|
160
|
|
|
|
161
|
|
|
/**
|
162
|
|
|
* SplDirectoryInfo::getPath
|
163
|
|
|
*
|
164
|
|
|
* Gets the path to the directory without trailing directory slash.
|
165
|
|
|
*
|
166
|
|
|
* @return string
|
167
|
|
|
*/
|
168
|
|
|
public function getPath()
|
169
|
|
|
{
|
170
|
|
|
return realpath($this->pathName);
|
171
|
|
|
}
|
172
|
|
|
|
173
|
|
|
// ------------------------------------------------------------------------
|
174
|
|
|
|
175
|
|
|
/**
|
176
|
|
|
* SplDirectoryInfo::getRealPath
|
177
|
|
|
*
|
178
|
|
|
* Gets the path to the directory
|
179
|
|
|
*
|
180
|
|
|
* @return string
|
181
|
|
|
*/
|
182
|
|
|
public function getRealPath()
|
183
|
|
|
{
|
184
|
|
|
return $this->pathName;
|
185
|
|
|
}
|
186
|
|
|
|
187
|
|
|
// ------------------------------------------------------------------------
|
188
|
|
|
|
189
|
|
|
/**
|
190
|
|
|
* SplDirectoryInfo::getRelativePath
|
191
|
|
|
*
|
192
|
|
|
* Gets the realtive path to the directory, for html link purposes.
|
193
|
|
|
*
|
194
|
|
|
* @return string
|
195
|
|
|
*/
|
196
|
|
|
public function getRelativePath()
|
197
|
|
|
{
|
198
|
|
|
$scriptFilename = str_replace(['/', '\\'], '/', dirname($_SERVER[ 'SCRIPT_FILENAME' ]));
|
199
|
|
|
$relativePath = str_replace(['/', '\\'], '/', $this->pathName);
|
200
|
|
|
|
201
|
|
|
if (strpos($scriptFilename, 'public')) {
|
202
|
|
|
return str_replace(dirname($scriptFilename), '..', $relativePath);
|
203
|
|
|
}
|
204
|
|
|
|
205
|
|
|
return str_replace($scriptFilename, '', $relativePath);
|
206
|
|
|
}
|
207
|
|
|
|
208
|
|
|
// ------------------------------------------------------------------------
|
209
|
|
|
|
210
|
|
|
/**
|
211
|
|
|
* SplDirectoryInfo::isReadable
|
212
|
|
|
*
|
213
|
|
|
* Determine if the directory is readable
|
214
|
|
|
*
|
215
|
|
|
* @return bool
|
216
|
|
|
*/
|
217
|
|
|
public function isReadable()
|
218
|
|
|
{
|
219
|
|
|
return (bool)is_dir($this->pathName);
|
220
|
|
|
}
|
221
|
|
|
|
222
|
|
|
// ------------------------------------------------------------------------
|
223
|
|
|
|
224
|
|
|
/**
|
225
|
|
|
* SplDirectoryInfo::isWritable
|
226
|
|
|
*
|
227
|
|
|
* Determine if the directory is writable
|
228
|
|
|
*
|
229
|
|
|
* @return bool
|
230
|
|
|
*/
|
231
|
|
|
public function isWritable()
|
232
|
|
|
{
|
233
|
|
|
// If we're on a Unix server with safe_mode off we call is_writable
|
234
|
|
|
if (DIRECTORY_SEPARATOR === '/' AND
|
235
|
|
|
(strpos(phpversion(), '5.4') !== false OR ! ini_get('safe_mode'))
|
236
|
|
|
) {
|
237
|
|
|
return (bool)is_writable($this->pathName);
|
238
|
|
|
}
|
239
|
|
|
|
240
|
|
|
/* For Windows servers and safe_mode "on" installations we'll actually
|
241
|
|
|
* write a file then read it. Bah...
|
242
|
|
|
*/
|
243
|
|
|
if (is_dir($this->pathName)) {
|
244
|
|
|
$file = $this->pathName . md5(mt_rand());
|
245
|
|
|
if (($fp = @fopen($file, 'ab')) === false) {
|
246
|
|
|
return false;
|
247
|
|
|
}
|
248
|
|
|
|
249
|
|
|
fclose($fp);
|
250
|
|
|
@chmod($file, 0777);
|
|
|
|
|
251
|
|
|
@unlink($file);
|
|
|
|
|
252
|
|
|
|
253
|
|
|
return true;
|
254
|
|
|
} elseif ( ! is_dir($this->pathName) OR ($fp = @fopen($this->pathName, 'ab')) === false) {
|
255
|
|
|
return false;
|
256
|
|
|
}
|
257
|
|
|
|
258
|
|
|
fclose($fp);
|
259
|
|
|
|
260
|
|
|
return true;
|
261
|
|
|
}
|
262
|
|
|
|
263
|
|
|
// ------------------------------------------------------------------------
|
264
|
|
|
|
265
|
|
|
/**
|
266
|
|
|
* SplDirectoryInfo::getATime
|
267
|
|
|
*
|
268
|
|
|
* Gets last access time of the directory.
|
269
|
|
|
*
|
270
|
|
|
* @return int
|
271
|
|
|
*/
|
272
|
|
|
public function getATime()
|
273
|
|
|
{
|
274
|
|
|
return fileatime($this->pathName);
|
275
|
|
|
}
|
276
|
|
|
|
277
|
|
|
// ------------------------------------------------------------------------
|
278
|
|
|
|
279
|
|
|
/**
|
280
|
|
|
* SplDirectoryInfo::getCTime
|
281
|
|
|
*
|
282
|
|
|
* Gets the inode change time of the directory.
|
283
|
|
|
*
|
284
|
|
|
* @return int
|
285
|
|
|
*/
|
286
|
|
|
public function getCTime()
|
287
|
|
|
{
|
288
|
|
|
return filectime($this->pathName);
|
289
|
|
|
}
|
290
|
|
|
|
291
|
|
|
// ------------------------------------------------------------------------
|
292
|
|
|
|
293
|
|
|
/**
|
294
|
|
|
* SplDirectoryInfo::getMTime
|
295
|
|
|
*
|
296
|
|
|
* Gets the last modified time of the directory.
|
297
|
|
|
*
|
298
|
|
|
* @return int
|
299
|
|
|
*/
|
300
|
|
|
public function getMTime()
|
301
|
|
|
{
|
302
|
|
|
return filemtime($this->pathName);
|
303
|
|
|
}
|
304
|
|
|
|
305
|
|
|
// ------------------------------------------------------------------------
|
306
|
|
|
|
307
|
|
|
/**
|
308
|
|
|
* SplDirectoryInfo::getInode
|
309
|
|
|
*
|
310
|
|
|
* Gets the inode number for the filesystem directory object.
|
311
|
|
|
*
|
312
|
|
|
* @return int
|
313
|
|
|
*/
|
314
|
|
|
public function getInode()
|
315
|
|
|
{
|
316
|
|
|
return fileinode($this->pathName);
|
317
|
|
|
}
|
318
|
|
|
|
319
|
|
|
// ------------------------------------------------------------------------
|
320
|
|
|
|
321
|
|
|
/**
|
322
|
|
|
* SplDirectoryInfo::getPerms
|
323
|
|
|
*
|
324
|
|
|
* Gets the permissions for the directory
|
325
|
|
|
*
|
326
|
|
|
* @param bool $octal
|
327
|
|
|
*
|
328
|
|
|
* @return int
|
329
|
|
|
*/
|
330
|
|
|
public function getPerms($octal = false)
|
331
|
|
|
{
|
332
|
|
|
return $octal === false ? fileperms($this->pathName) : 0 . decoct(fileperms($this->pathName) & 0777);
|
|
|
|
|
333
|
|
|
}
|
334
|
|
|
|
335
|
|
|
// ------------------------------------------------------------------------
|
336
|
|
|
|
337
|
|
|
/**
|
338
|
|
|
* SplDirectoryInfo::getOwner
|
339
|
|
|
*
|
340
|
|
|
* Gets the directory owner. The owner ID is can be returned in numerical format or string of owner id.
|
341
|
|
|
*
|
342
|
|
|
* @param bool $id
|
343
|
|
|
*
|
344
|
|
|
* @return array|int|string
|
345
|
|
|
*/
|
346
|
|
|
public function getOwner($id = false)
|
347
|
|
|
{
|
348
|
|
|
if ($id) {
|
349
|
|
|
if (false !== ($uid = fileowner($this->pathName))) {
|
350
|
|
|
if (function_exists('posix_getpwuid')) {
|
351
|
|
|
return posix_getpwuid($uid);
|
352
|
|
|
} elseif ($uid == 0) {
|
353
|
|
|
return 'root';
|
354
|
|
|
}
|
355
|
|
|
}
|
356
|
|
|
}
|
357
|
|
|
|
358
|
|
|
return fileowner($this->pathName);
|
359
|
|
|
}
|
360
|
|
|
|
361
|
|
|
// ------------------------------------------------------------------------
|
362
|
|
|
|
363
|
|
|
/**
|
364
|
|
|
* SplDirectoryInfo::getGroup
|
365
|
|
|
*
|
366
|
|
|
* Gets the directory group. The group ID is can be returned in numerical format or string of group id.
|
367
|
|
|
*
|
368
|
|
|
* @param bool $id
|
369
|
|
|
*
|
370
|
|
|
* @return array|int|string
|
371
|
|
|
*/
|
372
|
|
|
public function getGroup($id = false)
|
373
|
|
|
{
|
374
|
|
|
if ($id) {
|
375
|
|
|
if (false !== ($grid = fileowner($this->pathName))) {
|
376
|
|
|
if (function_exists('posix_getgrgid')) {
|
377
|
|
|
return posix_getgrgid($grid);
|
378
|
|
|
} elseif ($grid == 0) {
|
379
|
|
|
return 'root';
|
380
|
|
|
}
|
381
|
|
|
}
|
382
|
|
|
}
|
383
|
|
|
|
384
|
|
|
return filegroup($this->pathName);
|
385
|
|
|
}
|
386
|
|
|
|
387
|
|
|
// ------------------------------------------------------------------------
|
388
|
|
|
|
389
|
|
|
/**
|
390
|
|
|
* SplDirectoryInfo::getStat
|
391
|
|
|
*
|
392
|
|
|
* Gets the directory information.
|
393
|
|
|
*
|
394
|
|
|
* @return array
|
395
|
|
|
*/
|
396
|
|
|
public function getStat()
|
397
|
|
|
{
|
398
|
|
|
return stat($this->pathName);
|
|
|
|
|
399
|
|
|
}
|
400
|
|
|
|
401
|
|
|
// ------------------------------------------------------------------------
|
402
|
|
|
|
403
|
|
|
/**
|
404
|
|
|
* SplDirectoryInfo::getSize
|
405
|
|
|
*
|
406
|
|
|
* Gets the directory total size
|
407
|
|
|
*
|
408
|
|
|
* @return int
|
409
|
|
|
*/
|
410
|
|
|
public function getSize()
|
411
|
|
|
{
|
412
|
|
|
$size = 0;
|
413
|
|
|
|
414
|
|
|
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->pathName));
|
415
|
|
|
|
416
|
|
|
foreach ($files as $file) {
|
417
|
|
|
$size += $file->getSize();
|
418
|
|
|
}
|
419
|
|
|
|
420
|
|
|
return $size;
|
421
|
|
|
}
|
422
|
|
|
|
423
|
|
|
// ------------------------------------------------------------------------
|
424
|
|
|
|
425
|
|
|
/**
|
426
|
|
|
* SplDirectoryInfo::getTree
|
427
|
|
|
*
|
428
|
|
|
* Gets the directory files and directories tree.
|
429
|
|
|
*
|
430
|
|
|
* @return array
|
431
|
|
|
*/
|
432
|
|
|
public function getTree()
|
433
|
|
|
{
|
434
|
|
|
$tree = [];
|
435
|
|
|
$directoryIterator = new \DirectoryIterator($this->pathName);
|
436
|
|
|
|
437
|
|
|
foreach ($directoryIterator as $directoryNode) {
|
438
|
|
|
if ($directoryNode->isDir() AND $directoryNode->isDot() === false) {
|
439
|
|
|
$tree[ $directoryNode->getFilename() ] = (new SplDirectoryInfo(
|
440
|
|
|
$directoryNode->getPathName()
|
441
|
|
|
))->getTree();
|
442
|
|
|
} elseif ($directoryNode->isFile()) {
|
443
|
|
|
$tree[] = $directoryNode->getFilename();
|
444
|
|
|
}
|
445
|
|
|
}
|
446
|
|
|
|
447
|
|
|
arsort($tree, SORT_FLAG_CASE);
|
448
|
|
|
|
449
|
|
|
return $tree;
|
450
|
|
|
}
|
451
|
|
|
|
452
|
|
|
// ------------------------------------------------------------------------
|
453
|
|
|
|
454
|
|
|
/**
|
455
|
|
|
* SplDirectoryInfo::getHandle
|
456
|
|
|
*
|
457
|
|
|
* Returns the directory resource handle.
|
458
|
|
|
*
|
459
|
|
|
* @return resource
|
460
|
|
|
*/
|
461
|
|
|
public function getHandle()
|
462
|
|
|
{
|
463
|
|
|
return dir($this->pathName)->handle;
|
464
|
|
|
}
|
465
|
|
|
|
466
|
|
|
// ------------------------------------------------------------------------
|
467
|
|
|
|
468
|
|
|
/**
|
469
|
|
|
* SplDirectoryInfo::__toString
|
470
|
|
|
*
|
471
|
|
|
* This method will return the directory path name.
|
472
|
|
|
*
|
473
|
|
|
* @return string
|
474
|
|
|
*/
|
475
|
|
|
public function __toString()
|
476
|
|
|
{
|
477
|
|
|
return $this->pathName;
|
478
|
|
|
}
|
479
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: