/
File.php
361 lines (301 loc) · 8.17 KB
/
File.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/**
* 文件管理操作类
*
* 用于文件夹内容的读取,复制,剪切等操作
*
* @author tommy <tommy@doitphp.com>
* @copyright Copyright (c) 2010 Tommy Software Studio
* @link http://www.doitphp.com
* @license New BSD License.{@link http://www.opensource.org/licenses/bsd-license.php}
* @version $Id: File.php 2.0 2012-12-23 18:30:01Z tommy $
* @package library
* @since 1.0
*/
namespace doitphp\library;
use doitphp\core\Response;
if (!defined('IN_DOIT')) {
exit();
}
class File {
/**
* 分析目标目录的读写权限
*
* @access public
*
* @param string $dirPath 目标目录
* @param string $mod 权限值,默认:0755
*
* @return boolean
*/
public static function makeDir($dirPath, $mode = 0755) {
//参数分析
if (!$dirPath) {
return false;
}
if (is_dir($dirPath)) {
return true;
}
mkdir($dirPath, $mode, true);
return true;
}
/**
* 获取目录内文件
*
* @access public
*
* @param string $dirPath 所要读取内容的目录名
*
* @return string
*/
public static function readDir($dirPath) {
//参数分析
if (!$dirPath) {
return false;
}
//define filter file name
$filterArray = array('.cvs', '.svn', '.git');
$dir = self::_parseDir($dirPath);
$handle = opendir($dir);
$files = array();
while (false !== ($file = readdir($handle))) {
if ($file == '.' || $file == '..' || in_array($file, $filterArray)) {
continue;
}
$files[] = $file;
}
closedir($handle);
return $files;
}
/**
* 将一个文件夹内容复制到另一个文件夹
*
* @access public
*
* @param string $source 被复制的文件夹名
* @param string $dest 所要复制文件的目标文件夹
*
* @return boolean
*/
public static function copyDir($source, $dest) {
//参数分析
if (!$source || !$dest) {
return false;
}
$parseDir = self::_parseDir($source);
$destDir = self::_parseDir($dest, true);
$fileList = self::readDir($parseDir);
foreach ($fileList as $file) {
if (is_dir($parseDir . DS . $file)) {
self::copyDir($parseDir . DS . $file, $destDir . DS . $file);
} else {
copy($parseDir . DS . $file, $destDir . DS . $file);
}
}
return true;
}
/**
* 移动文件夹, 相当于WIN下的ctr+x(剪切操作)
*
* @access public
*
* @param string $source 原目录名
* @param string $dest 目标目录
*
* @return boolean
*/
public static function moveDir($source, $dest) {
//参数分析
if (!$source || !$dest) {
return false;
}
$parseDir = self::_parseDir($source);
$destDir = self::_parseDir($dest, true);
$fileList = self::readDir($parseDir);
foreach ($fileList as $file) {
if (is_dir($parseDir . DS . $file)) {
self::moveDir($parseDir . DS . $file, $destDir . DS . $file);
} else {
if (copy($parseDir . DS . $file, $destDir . DS . $file)) {
unlink($parseDir . DS . $file);
}
}
}
rmdir($parseDir);
return true;
}
/**
* 删除文件夹
*
* @access public
*
* @param string $dirPath 所要删除文件的路径
*
* @return boolean
*/
public static function deleteDir($dirPath) {
//参数分析
if (!$dirPath){
return false;
}
//清空子目录及内部文件
self::clearDir($dirPath);
rmdir($dirPath);
return true;
}
/**
* 清空文件夹内的文件及子目录
*
* @access public
*
* @param string $dirPath 所要清空内容的文件夹名称
* @param boolean $option 是否删除子目录, 注:当为false时,只删除子目录中的文件,目录不会删除
*
* @return boolean
*/
public static function clearDir($dirPath, $option = true) {
//参数分析
if (!$dirPath){
return false;
}
$parseDir = self::_parseDir($dirPath);
$fileList = self::readDir($parseDir);
foreach ($fileList as $file) {
if (is_dir($parseDir . DS . $file)) {
self::clearDir($parseDir . DS . $file, $option);
if ($option == true) {
rmdir($parseDir . DS . $file);
}
} else {
unlink($parseDir . DS . $file);
}
}
return true;
}
/**
* 分析文件夹是否存在
*
* @access protected
*
* @param string $dirPath 所要操作的文件目录名
* @param boolean $isMkdirStatus 是否创建目录
*
* @return string
*/
protected static function _parseDir($dirPath, $isMkdirStatus = false) {
//参数分析
if (!$dirPath) {
return false;
}
if ($isMkdirStatus === true) {
self::makeDir($dirPath, 0755);
} else {
if (!is_dir($dirPath)) {
Response::halt('The dir: ' . $dirPath . ' is not found!');
}
}
return $dirPath;
}
/**
* 文件写操作
*
* @access public
*
* @param string $filePath 文件路径
* @param string $content 文件内容
*
* @return boolean
*/
public static function writeFile($filePath, $content = '') {
//参数分析
if (!$filePath) {
return false;
}
//分析文件目录
self::_parseDir(dirname($filePath), true);
return file_put_contents($filePath, $content, LOCK_EX);
}
/**
* 文件复制
*
* @access public
*
* @param string $sourceFile 源文件(被复制的文件)
* @param string $destFile 所要复制的文件
*
* @return boolean
*/
public static function copyFile($sourceFile, $destFile) {
//参数分析
if (!$sourceFile || !$destFile) {
return false;
}
//文件及目录分析
if (!is_file($sourceFile)) {
Response::halt('The file: ' . $sourceFile . ' is not found!');
}
self::_parseDir(dirname($destFile), true);
return copy($sourceFile, $destFile);
}
/**
* 文件重命名或移动文件
*
* @access public
*
* @param string $sourceFile 源文件
* @param string $destFile 新文件名或路径
*
* @return boolean
*/
public static function moveFile($sourceFile, $destFile) {
//参数分析
if (!$sourceFile || !$destFile) {
return false;
}
//文件及目录分析
if (!is_file($sourceFile)) {
Response::halt('The file:' . $sourceFile . ' is not found!');
}
self::_parseDir(dirname($destFile), true);
return rename($sourceFile, $destFile);
}
/**
* 删除文件
*
* @access public
*
* @param string $filePath 文件路径
*
* @return boolean
*/
public static function deleteFile($filePath) {
//参数分析
if (!$filePath) {
return false;
}
//文件分析
if (!is_file($filePath)) {
return true;
}
return unlink($filePath);
}
/**
* 字节格式化 把字节数格式为 B K M G T 描述的大小
*
* @access public
*
* @param integer $bytes 文件大小
* @param integer $dec 小数点后的位数
*
* @return string
*/
public static function formatBytes($bytes, $dec = 2) {
$unitPow = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
$pos = 0;
while ($bytes >= 1024) {
$bytes /= 1024;
$pos++;
}
return round($bytes, $dec) . ' ' . $unitPow[$pos];
}
}