Dir.php
17.4 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<?php
// +----------------------------------------------------------------------
// | ThinkPHP
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$
/**
+------------------------------------------------------------------------------
* DirectoryIterator实现类 PHP5以上内置了DirectoryIterator类
+------------------------------------------------------------------------------
* @category ORG
* @package ORG
* @subpackage Io
* @author liu21st <liu21st@gmail.com>
* @version $Id$
+------------------------------------------------------------------------------
*/
namespace dir;
class Dir {//类定义开始
private $_values = array();
public $error = "";
/**
+----------------------------------------------------------
* 架构函数
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $path 目录路径
+----------------------------------------------------------
*/
function __construct($path, $pattern = '*') {
if (substr($path, -1) != "/")
$path .= "/";
$this->listFile($path, $pattern);
}
/**
+----------------------------------------------------------
* 取得目录下面的文件信息
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param mixed $pathname 路径
+----------------------------------------------------------
*/
function listFile($pathname, $pattern = '*') {
static $_listDirs = array();
$guid = md5($pathname . $pattern);
if (!isset($_listDirs[$guid])) {
$dir = array();
$list = glob($pathname . $pattern);
if(empty($list)){
return;
}
foreach ($list as $i => $file) {
//$dir[$i]['filename'] = basename($file);
//basename取中文名出问题.改用此方法
//编码转换.把中文的调整一下.
$dir[$i]['filename'] = preg_replace('/^.+[\\\\\\/]/', '', $file);
$dir[$i]['pathname'] = realpath($file);
$dir[$i]['owner'] = fileowner($file);
$dir[$i]['perms'] = fileperms($file);
$dir[$i]['inode'] = fileinode($file);
$dir[$i]['group'] = filegroup($file);
$dir[$i]['path'] = dirname($file);
$dir[$i]['atime'] = fileatime($file);
$dir[$i]['ctime'] = filectime($file);
$dir[$i]['size'] = filesize($file);
$dir[$i]['type'] = filetype($file);
$dir[$i]['ext'] = is_file($file) ? strtolower(substr(strrchr(basename($file), '.'), 1)) : '';
$dir[$i]['mtime'] = filemtime($file);
$dir[$i]['isDir'] = is_dir($file);
$dir[$i]['isFile'] = is_file($file);
$dir[$i]['isLink'] = is_link($file);
//$dir[$i]['isExecutable']= function_exists('is_executable')?is_executable($file):'';
$dir[$i]['isReadable'] = is_readable($file);
$dir[$i]['isWritable'] = is_writable($file);
}
$cmp_func = create_function('$a,$b', '
$k = "isDir";
if($a[$k] == $b[$k]) return 0;
return $a[$k]>$b[$k]?-1:1;
');
// 对结果排序 保证目录在前面
usort($dir, $cmp_func);
$this->_values = $dir;
$_listDirs[$guid] = $dir;
} else {
$this->_values = $_listDirs[$guid];
}
}
/**
+----------------------------------------------------------
* 返回数组中的当前元素(单元)
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return array
+----------------------------------------------------------
*/
function current($arr) {
if (!is_array($arr)) {
return false;
}
return current($arr);
}
/**
+----------------------------------------------------------
* 文件上次访问时间
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return integer
+----------------------------------------------------------
*/
function getATime() {
$current = $this->current($this->_values);
return $current['atime'];
}
/**
+----------------------------------------------------------
* 取得文件的 inode 修改时间
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return integer
+----------------------------------------------------------
*/
function getCTime() {
$current = $this->current($this->_values);
return $current['ctime'];
}
/**
+----------------------------------------------------------
* 遍历子目录文件信息
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return DirectoryIterator
+----------------------------------------------------------
*/
function getChildren() {
$current = $this->current($this->_values);
if ($current['isDir']) {
return new Dir($current['pathname']);
}
return false;
}
/**
+----------------------------------------------------------
* 取得文件名
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
function getFilename() {
$current = $this->current($this->_values);
return $current['filename'];
}
/**
+----------------------------------------------------------
* 取得文件的组
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return integer
+----------------------------------------------------------
*/
function getGroup() {
$current = $this->current($this->_values);
return $current['group'];
}
/**
+----------------------------------------------------------
* 取得文件的 inode
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return integer
+----------------------------------------------------------
*/
function getInode() {
$current = $this->current($this->_values);
return $current['inode'];
}
/**
+----------------------------------------------------------
* 取得文件的上次修改时间
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return integer
+----------------------------------------------------------
*/
function getMTime() {
$current = $this->current($this->_values);
return $current['mtime'];
}
/**
+----------------------------------------------------------
* 取得文件的所有者
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
function getOwner() {
$current = $this->current($this->_values);
return $current['owner'];
}
/**
+----------------------------------------------------------
* 取得文件路径,不包括文件名
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
function getPath() {
$current = $this->current($this->_values);
return $current['path'];
}
/**
+----------------------------------------------------------
* 取得文件的完整路径,包括文件名
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
function getPathname() {
$current = $this->current($this->_values);
return $current['pathname'];
}
/**
+----------------------------------------------------------
* 取得文件的权限
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return integer
+----------------------------------------------------------
*/
function getPerms() {
$current = $this->current($this->_values);
return $current['perms'];
}
/**
+----------------------------------------------------------
* 取得文件的大小
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return integer
+----------------------------------------------------------
*/
function getSize() {
$current = $this->current($this->_values);
return $current['size'];
}
/**
+----------------------------------------------------------
* 取得文件类型
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
function getType() {
$current = $this->current($this->_values);
return $current['type'];
}
/**
+----------------------------------------------------------
* 是否为目录
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
function isDir() {
$current = $this->current($this->_values);
return $current['isDir'];
}
/**
+----------------------------------------------------------
* 是否为文件
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
function isFile() {
$current = $this->current($this->_values);
return $current['isFile'];
}
/**
+----------------------------------------------------------
* 文件是否为一个符号连接
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
function isLink() {
$current = $this->current($this->_values);
return $current['isLink'];
}
/**
+----------------------------------------------------------
* 文件是否可以执行
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
function isExecutable() {
$current = $this->current($this->_values);
return $current['isExecutable'];
}
/**
+----------------------------------------------------------
* 文件是否可读
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
function isReadable() {
$current = $this->current($this->_values);
return $current['isReadable'];
}
/**
+----------------------------------------------------------
* 获取foreach的遍历方式
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
function getIterator() {
return new ArrayObject($this->_values);
}
// 返回目录的数组信息
function toArray() {
return $this->_values;
}
// 静态方法
/**
+----------------------------------------------------------
* 判断目录是否为空
+----------------------------------------------------------
* @access static
+----------------------------------------------------------
* @return void
+----------------------------------------------------------
*/
function isEmpty($directory) {
$handle = opendir($directory);
while (($file = readdir($handle)) !== false) {
if ($file != "." && $file != "..") {
closedir($handle);
return false;
}
}
closedir($handle);
return true;
}
/**
+----------------------------------------------------------
* 取得目录中的结构信息
+----------------------------------------------------------
* @access static
+----------------------------------------------------------
* @return void
+----------------------------------------------------------
*/
function getList($directory) {
return scandir($directory);
}
/**
+----------------------------------------------------------
* 删除目录(包括下面的文件)
+----------------------------------------------------------
* @access static
+----------------------------------------------------------
* @return void
+----------------------------------------------------------
*/
function delDir($directory, $subdir = true) {
if (is_dir($directory) == false) {
$this->error = "该目录是不存在!";
return false;
}
$handle = opendir($directory);
while (($file = readdir($handle)) !== false) {
if ($file != "." && $file != "..") {
is_dir("$directory/$file") ?
Dir::delDir("$directory/$file") :
unlink("$directory/$file");
}
}
if (readdir($handle) == false) {
closedir($handle);
rmdir($directory);
}
}
/**
+----------------------------------------------------------
* 删除目录下面的所有文件,但不删除目录
+----------------------------------------------------------
* @access static
+----------------------------------------------------------
* @return void
+----------------------------------------------------------
*/
function del($directory) {
if (is_dir($directory) == false) {
$this->error = "该目录是不存在!";
return false;
}
$handle = opendir($directory);
while (($file = readdir($handle)) !== false) {
if ($file != "." && $file != ".." && is_file("$directory/$file")) {
unlink("$directory/$file");
}
}
closedir($handle);
}
/**
+----------------------------------------------------------
* 复制目录
+----------------------------------------------------------
* @access static
+----------------------------------------------------------
* @return void
+----------------------------------------------------------
*/
function copyDir($source, $destination) {
if (is_dir($source) == false) {
$this->error = "源目录不存在!";
return false;
}
if (is_dir($destination) == false) {
mkdir($destination, 0700);
}
$handle = opendir($source);
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
is_dir("$source/$file") ?
Dir::copyDir("$source/$file", "$destination/$file") :
copy("$source/$file", "$destination/$file");
}
}
closedir($handle);
}
}