1: <?php
2:
3: namespace ngatngay;
4:
5: use FilesystemIterator;
6: use RecursiveCallbackFilterIterator;
7: use RecursiveDirectoryIterator;
8: use RecursiveIteratorIterator;
9:
10: // file system
11: class fs
12: {
13: /*
14: * file, file1, file2...
15: */
16: function nameIncrement(string $file_name_body, string $file_ext): string
17: {
18: $i = 1;
19: $file_exists = true;
20:
21: do {
22: $file_save = $file_name_body . $i . '.' . $file_ext;
23:
24: if (!file_exists($file_save)) {
25: $file_exists = false;
26: }
27:
28: $i++;
29: } while ($file_exists);
30:
31: return $file_save;
32: }
33:
34: public static function getExtension(string $name): string
35: {
36: return (new \SplFileInfo($name))->getExtension();
37: }
38:
39: /**
40: * @param $fileSize string
41: * @return string
42: */
43: public static function readableSize($fileSize)
44: {
45: $size = floatval($fileSize);
46:
47: if ($size < 1024) {
48: $s = $size . ' B';
49: } elseif ($size < 1048576) {
50: $s = round($size / 1024, 2) . ' KB';
51: } elseif ($size < 1073741824) {
52: $s = round($size / 1048576, 2) . ' MB';
53: } elseif ($size < 1099511627776) {
54: $s = round($size / 1073741824, 2) . ' GB';
55: } elseif ($size < 1125899906842624) {
56: $s = round($size / 1099511627776, 2) . ' TB';
57: } elseif ($size < 1152921504606846976) {
58: $s = round($size / 1125899906842624, 2) . ' PB';
59: } elseif ($size < 1.1805916207174E+21) {
60: $s = round($size / 1152921504606846976, 2) . ' EB';
61: } elseif ($size < 1.2089258196146E+24) {
62: $s = round($size / 1.1805916207174E+21, 2) . ' ZB';
63: } else {
64: $s = round($size / 1.2089258196146E+24, 2) . ' YB';
65: }
66:
67: return $s;
68: }
69:
70: public static function remove(string $path): bool
71: {
72: if (is_link($path)) {
73: return unlink($path);
74: }
75:
76: if (is_file($path)) {
77: return unlink($path);
78: }
79:
80: if (is_dir($path)) {
81: $files = array_diff(scandir($path), ['.', '..']);
82: foreach ($files as $file) {
83: $filePath = $path . DIRECTORY_SEPARATOR . $file;
84: if (!self::remove($filePath)) {
85: return false;
86: }
87: }
88: return rmdir($path);
89: }
90:
91: if (!file_exists($path)) {
92: return true;
93: }
94:
95: throw new \Exception('remove error, not match file type');
96: }
97:
98: public static function read_full_dir($path, $excludes = [])
99: {
100: $directory = new RecursiveDirectoryIterator(
101: $path,
102: FilesystemIterator::UNIX_PATHS
103: | FilesystemIterator::SKIP_DOTS
104: );
105:
106: $filter = new RecursiveCallbackFilterIterator($directory, function ($current, $key, $iterator) use ($path, $excludes) {
107: $relativePath = str::replace_first($path, '', $current->getPathname());
108:
109: foreach ($excludes as $exclude) {
110: if (empty($exclude)) {
111: continue;
112: }
113: //var_dump($relativePath);
114: //var_dump($exclude);
115:
116: $exclude = trim($exclude);
117: $exclude = trim($exclude, '/');
118: $relativePath = trim($relativePath, '/');
119:
120: if (str_ends_with($relativePath, $exclude)) {
121: return false;
122: }
123: }
124:
125: return true;
126: });
127:
128: return new RecursiveIteratorIterator(
129: $filter,
130: RecursiveIteratorIterator::SELF_FIRST
131: );
132: }
133: }
134: