|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* abstract Class Base |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://www.icy2003.com/ |
|
6
|
|
|
* @author icy2003 <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2017, icy2003 |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace icy2003\php\icomponents\file; |
|
10
|
|
|
use icy2003\php\ihelpers\Charset; |
|
11
|
|
|
use icy2003\php\ihelpers\Arrays; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* 文件抽象类 |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class Base |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* 获得命令返回值 |
|
20
|
|
|
* |
|
21
|
|
|
* - 不要依赖这个,一些环境不一定支持 |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $command 命令 |
|
24
|
|
|
* |
|
25
|
|
|
* @return string|false |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract public function getCommandResult($command); |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @ignore |
|
31
|
|
|
*/ |
|
32
|
2 |
|
public function getBasename($path, $suffix = null) |
|
33
|
|
|
{ |
|
34
|
2 |
|
$path = str_replace('\\', '/', $path); |
|
35
|
2 |
|
return basename($path, $suffix); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @ignore |
|
40
|
|
|
*/ |
|
41
|
5 |
|
public function getDirname($path) |
|
42
|
|
|
{ |
|
43
|
5 |
|
$path = str_replace('\\', '/', $path); |
|
44
|
5 |
|
return dirname($path); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @ignore |
|
49
|
|
|
*/ |
|
50
|
|
|
abstract public function isFile($file); |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @ignore |
|
54
|
|
|
*/ |
|
55
|
|
|
abstract public function isDir($dir); |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* 返回规范化的绝对路径名 |
|
59
|
|
|
* |
|
60
|
|
|
* - 不支持 realpath 的类将使用这个,其他的由子类实现 |
|
61
|
|
|
* - 该函数实现是:处理输入的 path 中的 '/./', '/../' 以及多余的 '/' |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $path 要检查的路径 |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getRealpath($path) |
|
68
|
|
|
{ |
|
69
|
|
|
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); |
|
70
|
|
|
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen'); |
|
71
|
|
|
$absolutes = []; |
|
72
|
|
|
foreach ($parts as $part) { |
|
73
|
|
|
if ('.' == $part) { |
|
74
|
|
|
continue; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ('..' == $part) { |
|
78
|
|
|
array_pop($absolutes); |
|
79
|
|
|
} else { |
|
80
|
|
|
$absolutes[] = $part; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
return implode(DIRECTORY_SEPARATOR, $absolutes); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* 列出指定路径中的文件和目录 |
|
88
|
|
|
* |
|
89
|
|
|
* - 优先返回叶节点 |
|
90
|
|
|
* - 不包含“.”和“..” |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $dir 目录的路径,如不给,则使用当前路径(pwd) |
|
93
|
|
|
* @param integer $flags 选项设置,默认 FileConstants::COMPLETE_PATH,可支持参数: |
|
94
|
|
|
* - FileConstants::COMPLETE_PATH:使用完整路径 |
|
95
|
|
|
* - FileConstants::COMPLETE_PATH_DISABLED:不使用完整路径 |
|
96
|
|
|
* - FileConstants::RECURSIVE:递归遍历 |
|
97
|
|
|
* - FileConstants::RECURSIVE_DISABLED:不递归 |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
|
|
abstract public function getLists($dir = null, $flags = FileConstants::COMPLETE_PATH); |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* 取得文件大小 |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $file 文件的路径 |
|
107
|
|
|
* |
|
108
|
|
|
* @return integer |
|
109
|
|
|
*/ |
|
110
|
|
|
abstract public function getFilesize($file); |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* 将整个文件读入一个字符串 |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $file 要读取的文件的名称 |
|
116
|
|
|
* |
|
117
|
|
|
* @return string|false |
|
118
|
|
|
*/ |
|
119
|
|
|
abstract public function getFileContent($file); |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* 创建一个文件(目录会被递归地创建),并用字符串(资源)填充进文件 |
|
123
|
|
|
* |
|
124
|
|
|
* - 可将字符串、资源、数组写入文件,行为和 file_put_contents 一样 |
|
125
|
|
|
* - 资源:`$fp = fopen('https://www.icy2003.com', 'r');` |
|
126
|
|
|
* |
|
127
|
|
|
* @link https://www.php.net/manual/zh/function.file-put-contents.php |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $file 文件的路径 |
|
130
|
|
|
* @param string|array $string 待填充进文件的字符串(资源) |
|
131
|
|
|
* @param integer $mode 默认的 mode 是 0777,意味着最大可能的访问权 |
|
132
|
|
|
* |
|
133
|
|
|
* @return boolean |
|
134
|
|
|
*/ |
|
135
|
|
|
abstract public function putFileContent($file, $string, $mode = 0777); |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* 递归地创建目录 |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $dir 目录的路径 |
|
141
|
|
|
* @param integer $mode 默认的 mode 是 0777,意味着最大可能的访问权 |
|
142
|
|
|
* |
|
143
|
|
|
* @return boolean |
|
144
|
|
|
*/ |
|
145
|
4 |
|
public function createDir($dir, $mode = 0777) |
|
146
|
|
|
{ |
|
147
|
4 |
|
if ($this->isDir($dir)) { |
|
148
|
4 |
|
return true; |
|
149
|
|
|
} |
|
150
|
1 |
|
$this->createDir($this->getDirname($dir), $mode); |
|
151
|
1 |
|
return $this->_mkdir($dir, $mode); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* 删除一个文件 |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $file 文件的路径 |
|
158
|
|
|
* |
|
159
|
|
|
* @return boolean |
|
160
|
|
|
*/ |
|
161
|
|
|
abstract public function deleteFile($file); |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* 递归地删除目录 |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $dir 目录的路径 |
|
167
|
|
|
* @param boolean $deleteRoot 是否删除目录的根节点,默认 true,即删除 |
|
168
|
|
|
* |
|
169
|
|
|
* @return boolean |
|
170
|
|
|
*/ |
|
171
|
1 |
|
public function deleteDir($dir, $deleteRoot = true) |
|
172
|
|
|
{ |
|
173
|
1 |
|
if (false === $this->isDir($dir)) { |
|
174
|
|
|
return true; |
|
175
|
|
|
} |
|
176
|
1 |
|
$files = $this->getLists($dir, FileConstants::COMPLETE_PATH); |
|
177
|
1 |
|
foreach ($files as $file) { |
|
178
|
1 |
|
$this->isDir($file) ? $this->deleteDir($file) : $this->deleteFile($file); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
1 |
|
return true === $deleteRoot ? $this->_rmdir($dir) : true; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* 复制文件(目录会被递归地创建) |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $fromFile 文件原路径 |
|
188
|
|
|
* @param string $toFile 文件目标路径 |
|
189
|
|
|
* @param boolean $overwrite 已存在的文件是否被覆盖,默认 false,即不覆盖 |
|
190
|
|
|
* |
|
191
|
|
|
* @return boolean |
|
192
|
|
|
*/ |
|
193
|
3 |
|
public function copyFile($fromFile, $toFile, $overwrite = false) |
|
194
|
|
|
{ |
|
195
|
3 |
|
if (false === $this->isFile($fromFile)) { |
|
196
|
|
|
return false; |
|
197
|
|
|
} |
|
198
|
3 |
|
if ($this->isFile($toFile)) { |
|
199
|
|
|
if (false === $overwrite) { |
|
200
|
|
|
return false; |
|
201
|
|
|
} else { |
|
202
|
|
|
$this->deleteFile($toFile); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
3 |
|
$this->createDir($this->getDirname($toFile)); |
|
206
|
3 |
|
return $this->_copy($fromFile, $toFile); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* 递归地复制目录 |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $fromDir 目录原路径 |
|
213
|
|
|
* @param string $toDir 目录目标路径 |
|
214
|
|
|
* @param boolean $overwrite 已存在的目录是否被覆盖,默认 false,即不覆盖 |
|
215
|
|
|
* |
|
216
|
|
|
* @return boolean |
|
217
|
|
|
*/ |
|
218
|
1 |
|
public function copyDir($fromDir, $toDir, $overwrite = false) |
|
219
|
|
|
{ |
|
220
|
1 |
|
$fromDir = rtrim($fromDir, '/') . '/'; |
|
221
|
1 |
|
$toDir = rtrim($toDir, '/') . '/'; |
|
222
|
1 |
|
if (false === $this->isDir($fromDir)) { |
|
223
|
|
|
return false; |
|
224
|
|
|
} |
|
225
|
1 |
|
$this->createDir($toDir); |
|
226
|
1 |
|
$files = $this->getLists($fromDir, FileConstants::COMPLETE_PATH_DISABLED); |
|
227
|
1 |
|
foreach ($files as $file) { |
|
228
|
1 |
|
if ($this->isDir($fromDir . $file)) { |
|
229
|
1 |
|
$this->copyDir($fromDir . $file, $toDir . $file, $overwrite); |
|
230
|
|
|
} else { |
|
231
|
1 |
|
$this->copyFile($fromDir . $file, $toDir . $file, $overwrite); |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
1 |
|
return true; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* 移动文件(目录会被递归地创建) |
|
239
|
|
|
* |
|
240
|
|
|
* @param string $fromFile 文件原路径 |
|
241
|
|
|
* @param string $toFile 文件目标路径 |
|
242
|
|
|
* @param boolean $overwrite 已存在的文件是否被覆盖,默认 false,即不覆盖 |
|
243
|
|
|
* |
|
244
|
|
|
* @return boolean |
|
245
|
|
|
*/ |
|
246
|
1 |
|
public function moveFile($fromFile, $toFile, $overwrite = false) |
|
247
|
|
|
{ |
|
248
|
1 |
|
if (false === $this->isFile($fromFile)) { |
|
249
|
|
|
return false; |
|
250
|
|
|
} |
|
251
|
1 |
|
if ($this->isFile($toFile)) { |
|
252
|
|
|
if (false === $overwrite) { |
|
253
|
|
|
return false; |
|
254
|
|
|
} else { |
|
255
|
|
|
$this->deleteFile($toFile); |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
1 |
|
$this->createDir($this->getDirname($toFile)); |
|
259
|
1 |
|
return $this->_move($fromFile, $toFile); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* 递归地移动目录 |
|
264
|
|
|
* |
|
265
|
|
|
* @param string $fromDir 目录原路径 |
|
266
|
|
|
* @param string $toDir 目录目标路径 |
|
267
|
|
|
* @param boolean $overwrite 已存在的目录是否被覆盖,默认 false,即不覆盖 |
|
268
|
|
|
* |
|
269
|
|
|
* @return boolean |
|
270
|
|
|
*/ |
|
271
|
|
|
public function moveDir($fromDir, $toDir, $overwrite = false) |
|
272
|
|
|
{ |
|
273
|
|
|
$fromDir = rtrim($fromDir, '/') . '/'; |
|
274
|
|
|
$toDir = rtrim($toDir, '/') . '/'; |
|
275
|
|
|
if (false === $this->isDir($fromDir)) { |
|
276
|
|
|
return false; |
|
277
|
|
|
} |
|
278
|
|
|
$this->createDir($toDir); |
|
279
|
|
|
$files = $this->getLists($fromDir, FileConstants::COMPLETE_PATH_DISABLED); |
|
280
|
|
|
foreach ($files as $file) { |
|
281
|
|
|
if ($this->isDir($fromDir . $file)) { |
|
282
|
|
|
$this->moveDir($fromDir . $file, $toDir . $file, $overwrite); |
|
283
|
|
|
} else { |
|
284
|
|
|
$this->moveFile($fromDir . $file, $toDir . $file, $overwrite); |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
return $this->deleteDir($fromDir); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* 上传文件 |
|
292
|
|
|
* |
|
293
|
|
|
* @param string|array $fileMap @see self::fileMap() |
|
294
|
|
|
* @param boolean $overwrite 是否覆盖,默认 true,即:是 |
|
295
|
|
|
* |
|
296
|
|
|
* @return boolean |
|
297
|
|
|
*/ |
|
298
|
|
|
abstract public function uploadFile($fileMap, $overwrite = true); |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* 下载文件 |
|
302
|
|
|
* |
|
303
|
|
|
* @param string|array $fileMap @see self::fileMap() |
|
304
|
|
|
* @param boolean $overwrite 是否覆盖,默认 true,即:是 |
|
305
|
|
|
* |
|
306
|
|
|
* @return boolean |
|
307
|
|
|
*/ |
|
308
|
|
|
abstract public function downloadFile($fileMap, $overwrite = true); |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* 改变文件(目录)的创建者 |
|
312
|
|
|
* |
|
313
|
|
|
* - 不支持的类:FtpFile |
|
314
|
|
|
* |
|
315
|
|
|
* @param string $file 文件或者目录 |
|
316
|
|
|
* @param string $user 创建者 |
|
317
|
|
|
* @param integer $flags 选项设置,默认 FileConstants::RECURSIVE_DISABLED,可支持参数: |
|
318
|
|
|
* - FileConstants::RECURSIVE_DISABLED |
|
319
|
|
|
* - FileConstants::RECURSIVE |
|
320
|
|
|
* |
|
321
|
|
|
* @return boolean |
|
322
|
|
|
*/ |
|
323
|
|
|
abstract public function chown($file, $user, $flags = FileConstants::RECURSIVE_DISABLED); |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* 改变文件(目录)的群组 |
|
327
|
|
|
* |
|
328
|
|
|
* - 不支持的类:FtpFile |
|
329
|
|
|
* |
|
330
|
|
|
* @param string $file 文件或者目录 |
|
331
|
|
|
* @param string $group 群组 |
|
332
|
|
|
* @param integer $flags 选项设置,默认 FileConstants::RECURSIVE_DISABLED,可支持参数: |
|
333
|
|
|
* - FileConstants::RECURSIVE_DISABLED |
|
334
|
|
|
* - FileConstants::RECURSIVE |
|
335
|
|
|
* |
|
336
|
|
|
* @return boolean |
|
337
|
|
|
*/ |
|
338
|
|
|
abstract public function chgrp($file, $group, $flags = FileConstants::RECURSIVE_DISABLED); |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* 改变文件(目录)的安全模式 |
|
342
|
|
|
* |
|
343
|
|
|
* @param string $file 文件或者目录 |
|
344
|
|
|
* @param integer $mode 默认的 mode 是 0777,意味着最大可能的访问权 |
|
345
|
|
|
* @param integer $flags 选项设置,默认 FileConstants::RECURSIVE_DISABLED,可支持参数: |
|
346
|
|
|
* - FileConstants::RECURSIVE_DISABLED |
|
347
|
|
|
* - FileConstants::RECURSIVE |
|
348
|
|
|
* @return boolean |
|
349
|
|
|
*/ |
|
350
|
|
|
abstract public function chmod($file, $mode = 0777, $flags = FileConstants::RECURSIVE_DISABLED); |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* 建立符号连接 |
|
354
|
|
|
* |
|
355
|
|
|
* - 不支持的类:FtpFile |
|
356
|
|
|
* |
|
357
|
|
|
* @param string $from 连接的目标 |
|
358
|
|
|
* @param string $to 连接的名称 |
|
359
|
|
|
* |
|
360
|
|
|
* @return boolean |
|
361
|
|
|
*/ |
|
362
|
|
|
abstract public function symlink($from, $to); |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* 关闭文件句柄(连接) |
|
366
|
|
|
* |
|
367
|
|
|
* @return boolean |
|
368
|
|
|
*/ |
|
369
|
|
|
abstract public function close(); |
|
370
|
|
|
|
|
371
|
|
|
/** |
|
372
|
|
|
* 非递归地复制目录(文件) |
|
373
|
|
|
* |
|
374
|
|
|
* @param string $fromFile 源目录(文件)的路径 |
|
375
|
|
|
* @param string $toFile 目标目录(文件)的路径 |
|
376
|
|
|
* |
|
377
|
|
|
* @return boolean |
|
378
|
|
|
*/ |
|
379
|
|
|
abstract protected function _copy($fromFile, $toFile); |
|
380
|
|
|
|
|
381
|
|
|
/** |
|
382
|
|
|
* 非递归地移动目录(文件) |
|
383
|
|
|
* |
|
384
|
|
|
* @param string $fromFile 源目录(文件)的路径 |
|
385
|
|
|
* @param string $toFile 目标目录(文件)的路径 |
|
386
|
|
|
* |
|
387
|
|
|
* @return boolean |
|
388
|
|
|
*/ |
|
389
|
|
|
abstract protected function _move($fromFile, $toFile); |
|
390
|
|
|
|
|
391
|
|
|
/** |
|
392
|
|
|
* 非递归地创建目录 |
|
393
|
|
|
* |
|
394
|
|
|
* @param string $dir 目录的路径 |
|
395
|
|
|
* @param integer $mode 默认的 mode 是 0777,意味着最大可能的访问权 |
|
396
|
|
|
* |
|
397
|
|
|
* @return boolean |
|
398
|
|
|
*/ |
|
399
|
|
|
abstract protected function _mkdir($dir, $mode = 0777); |
|
400
|
|
|
|
|
401
|
|
|
/** |
|
402
|
|
|
* 非递归地删除目录 |
|
403
|
|
|
* |
|
404
|
|
|
* @param string $dir |
|
405
|
|
|
* |
|
406
|
|
|
* @return boolean |
|
407
|
|
|
*/ |
|
408
|
|
|
abstract protected function _rmdir($dir); |
|
409
|
|
|
|
|
410
|
|
|
/** |
|
411
|
|
|
* 析构函数:关闭文件句柄(连接) |
|
412
|
|
|
*/ |
|
413
|
52 |
|
public function __destruct() |
|
414
|
|
|
{ |
|
415
|
52 |
|
$this->close(); |
|
416
|
52 |
|
} |
|
417
|
|
|
|
|
418
|
|
|
/** |
|
419
|
|
|
* 返回文件映射 |
|
420
|
|
|
* |
|
421
|
|
|
* - 支持别名 |
|
422
|
|
|
* |
|
423
|
|
|
* @param string|array $file 数组:[[带路径的文件名],[不带路径的文件名]],字符串会转成数组 |
|
424
|
|
|
* |
|
425
|
|
|
* @return array |
|
426
|
|
|
*/ |
|
427
|
1 |
|
public function fileMap($file) |
|
428
|
|
|
{ |
|
429
|
1 |
|
if (is_string($file)) { |
|
430
|
|
|
$file = [$file, Charset::toCn($this->getBasename($file))]; |
|
431
|
1 |
|
} elseif (is_array($file)) { |
|
|
|
|
|
|
432
|
1 |
|
$file = Arrays::lists($file, 2); |
|
433
|
1 |
|
if ($this->isDir($file[1])) { |
|
434
|
1 |
|
$file[1] = rtrim($file[1], '/') . '/' . Charset::toCn($this->getBasename($file[0])); |
|
435
|
|
|
} |
|
436
|
|
|
} else { |
|
437
|
|
|
$file = ['', '']; |
|
438
|
|
|
} |
|
439
|
1 |
|
return $file; |
|
440
|
|
|
} |
|
441
|
|
|
} |
|
442
|
|
|
|