|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.tintsoft.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd. |
|
5
|
|
|
* @license http://www.tintsoft.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace yuncms\helpers; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class FileHelper |
|
12
|
|
|
* |
|
13
|
|
|
* @author Tongle Xu <[email protected]> |
|
14
|
|
|
* @since 3.0 |
|
15
|
|
|
*/ |
|
16
|
|
|
class FileHelper extends \yii\helpers\FileHelper |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string the path (or alias) of a PHP file containing MIME type information. |
|
20
|
|
|
*/ |
|
21
|
|
|
public static $mimeMagicFile = '@yuncms/helpers/mimeTypes.php'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Checks if given fileName has a extension |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $fileName the filename |
|
27
|
|
|
* @return boolean has extension |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function hasExtension(string $fileName): bool |
|
30
|
|
|
{ |
|
31
|
|
|
return (strpos($fileName, ".") !== false); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Determine if a file exists. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $path |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function exists(string $path): bool |
|
41
|
|
|
{ |
|
42
|
|
|
return file_exists($path); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Write the contents of a file. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $path |
|
49
|
|
|
* @param string $contents |
|
50
|
|
|
* @param bool $lock |
|
51
|
|
|
* @return int |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function put(string $path, string $contents, bool $lock = false): int |
|
54
|
|
|
{ |
|
55
|
|
|
return file_put_contents($path, $contents, $lock ? LOCK_EX : 0); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Append to a file. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $path |
|
62
|
|
|
* @param string $data |
|
63
|
|
|
* @return int |
|
64
|
|
|
*/ |
|
65
|
|
|
public function append(string $path, string $data): int |
|
66
|
|
|
{ |
|
67
|
|
|
return file_put_contents($path, $data, FILE_APPEND); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Delete a file. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $path |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function delete(string $path): bool |
|
77
|
|
|
{ |
|
78
|
|
|
if (static::exists($path)) { |
|
79
|
|
|
return @unlink($path); |
|
80
|
|
|
} |
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* 移动文件到新位置 |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $path 原始路径 |
|
88
|
|
|
* @param string $target 新路径 |
|
89
|
|
|
* @return bool true on success or false on failure. |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function move(string $path, string $target): bool |
|
92
|
|
|
{ |
|
93
|
|
|
return rename($path, $target); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* 复制文件到新位置 |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $path 原始路径 |
|
100
|
|
|
* @param string $target 新路径 |
|
101
|
|
|
* @return bool true on success or false on failure. |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function copy(string $path, string $target): bool |
|
104
|
|
|
{ |
|
105
|
|
|
return copy($path, $target); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get the file type of a given file. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $path |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
public static function type(string $path): string |
|
115
|
|
|
{ |
|
116
|
|
|
return filetype($path); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get the file size of a given file. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $path |
|
123
|
|
|
* @return int |
|
124
|
|
|
*/ |
|
125
|
|
|
public static function size(string $path): int |
|
126
|
|
|
{ |
|
127
|
|
|
return filesize($path); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* 获取文件的最后修改时间 |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $path |
|
134
|
|
|
* @return int |
|
135
|
|
|
*/ |
|
136
|
|
|
public static function lastModified(string $path): int |
|
137
|
|
|
{ |
|
138
|
|
|
return filemtime($path); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param string $path |
|
143
|
|
|
* @return string original file base name |
|
144
|
|
|
*/ |
|
145
|
|
|
public static function baseName(string $path): string |
|
146
|
|
|
{ |
|
147
|
|
|
// https://github.com/yiisoft/yii2/issues/11012 |
|
148
|
|
|
return mb_substr(pathinfo('_' . $path, PATHINFO_FILENAME), 1, null, '8bit'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param string $path |
|
153
|
|
|
* @return string file extension |
|
154
|
|
|
*/ |
|
155
|
|
|
public static function extension(string $path): string |
|
156
|
|
|
{ |
|
157
|
|
|
return strtolower(pathinfo($path, PATHINFO_EXTENSION)); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* 使用RecursiveDirectoryIterator遍历文件,列出所有文件路径 |
|
162
|
|
|
* @param \RecursiveDirectoryIterator|string $directory 指定了目录的RecursiveDirectoryIterator实例 |
|
163
|
|
|
* @return array $files 文件列表 |
|
164
|
|
|
*/ |
|
165
|
|
|
public static function files(\RecursiveDirectoryIterator $directory): array |
|
166
|
|
|
{ |
|
167
|
|
|
if (!$directory instanceof \RecursiveDirectoryIterator) { |
|
|
|
|
|
|
168
|
|
|
$directory = new \RecursiveDirectoryIterator($directory); |
|
169
|
|
|
} |
|
170
|
|
|
$files = []; |
|
171
|
|
|
for (; $directory->valid(); $directory->next()) { |
|
172
|
|
|
if ($directory->isDir() && !$directory->isDot()) { |
|
173
|
|
|
if ($directory->haschildren()) { |
|
174
|
|
|
$files = array_merge($files, static::files($directory->getChildren())); |
|
175
|
|
|
}; |
|
176
|
|
|
} else if ($directory->isFile()) { |
|
177
|
|
|
$files[] = $directory->getPathName(); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
return $files; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Empty the specified directory of all files and folders. |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $directory |
|
187
|
|
|
* @return bool |
|
188
|
|
|
* @throws \yii\base\ErrorException |
|
189
|
|
|
* @throws \yii\base\Exception |
|
190
|
|
|
*/ |
|
191
|
|
|
public static function cleanDirectory(string $directory): bool |
|
192
|
|
|
{ |
|
193
|
|
|
self::removeDirectory($directory); |
|
194
|
|
|
return self::createDirectory($directory); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Returns whether the file path is an absolute path. |
|
199
|
|
|
* |
|
200
|
|
|
* @param string $path A file path |
|
201
|
|
|
* @return bool |
|
202
|
|
|
*/ |
|
203
|
|
|
public static function isAbsolutePath($path): bool |
|
204
|
|
|
{ |
|
205
|
|
|
return strspn($path, '/\\', 0, 1) |
|
206
|
|
|
|| (strlen($path) > 3 && ctype_alpha($path[0]) |
|
207
|
|
|
&& ':' === $path[1] |
|
208
|
|
|
&& strspn($path, '/\\', 2, 1) |
|
209
|
|
|
) |
|
210
|
|
|
|| null !== parse_url($path, PHP_URL_SCHEME) |
|
211
|
|
|
; |
|
212
|
|
|
} |
|
213
|
|
|
} |