-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtendedSPLFileInfo.class.php
77 lines (67 loc) · 1.78 KB
/
ExtendedSPLFileInfo.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* ExtendedSPLFileInfo
*
* @author Rob Lambell <[email protected]>
* @license MIT
*/
class ExtendedSPLFileInfo extends SPLFileInfo
{
/**
* @param $filename
*/
public function __construct($filename) {
parent::__construct($filename);
}
/**
* @return int
*/
function getRealSize()
{
if ($this->isDir()) {
$path = $this->getPathname();
} else {
return $this->getSize();
}
$size = 0;
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS));
foreach($files as $file){
$size += $file->getSize();
}
return $size;
}
/**
* @return int
*/
function getFileMTime()
{
if ($this->isDir()) {
$path = $this->getPathname();
} else {
return $this->getMTime();
}
$lastModified = 0;
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS));
foreach ($files as $file) {
if ($file->getMTime() > $lastModified) {
$lastModified = $file->getMTime();
}
}
if ($lastModified < 1) {
$lastModified = $this->getMTime();
}
return $lastModified;
}
/**
* @param bool $bytes
* @param int $decimals
* @return string
*/
public function getHumanSize($bytes = false, $decimals = 2)
{
$bytes = $bytes ? $bytes : $this->getRealSize();
$size = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f ", $bytes / pow(1024, $factor)) . @$size[$factor];
}
}