项目需要,需要写一个函数,能够遍历指定目录中的所有文件,而且这个目录中的子目录也要遍历。输出文件的属性信息,并存储。
想想需求,不就是一个ls -al命令吗,实现获取相关属性就好了,再加上一个遍历ok了事。
项目过程中,便于操作,使用了json格式存储,但是也发现了一些问题。谨此记录问题及代码,便于参考。
?
|
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
|
<?php
define('indexformat',"dir,name,size,perms,ower,group,ctime,mtime,atime,suffix") ;
define('indextxt', 'data/index.txt');
define('indexjson', 'data/index.json');
date_default_timezone_set('asia/hong_kong');
if (file_exists(indextxt)) {
unlink(indextxt);
}
$dir = './';
getindexfile($dir);
/*
* get index file
* @filename indextxt
* @dir string
*
*/
function getindexfile($dir,$whitelist=''){
$string = '';
$dir = trim($dir);
$dir = realpath($dir);
$dir = $dir."//";
if(is_file($dir)){
putindexfile($dir);
}else{
putindexfile($dir);
$odir = @opendir($dir);
while($filename = readdir($odir)){
if($filename!='.' && $filename!='..'){
if(is_file($dir.$filename)){
putindexfile($dir.$filename);
}elseif(is_dir($dir.$filename)){
getindexfile($dir.$filename);
}
}
}
}
if (!file_exists(indextxt)) {
return false;
}
//$data = json_encode(getindexfromfile());
//file_put_contents(indexjson,$data);
return true;
}
/*
* get index file
* @filename file.index
*
*/
function putindexfile($file){
if (!file_exists($file)) {
return false;
}
$format = explode(',', indexformat);
$string = "";
foreach ($format as $key => $value) {
if($key !== 0 ){
$string .= "\\t";
}
$string .= getfileattr($file,$value);
}
$string .= "\\n";
file_put_contents(indextxt, $string, file_append);
}
/*
*
* get index string from index file
* @return array()
*
*/
function getindexfromfile($flag=''){
if (!file_exists(indextxt)) {
return false;
}
$arr = file(indextxt);
$format = explode(',', indexformat);
$result = array();
if(!empty($flag)){
$key = array_search($flag, $format);
if ($key === false) {
return false;
}
foreach ($arr as $str) {
$tmp = explode(" ", trim($str));
$result[$flag][] = $tmp[$key];
}
}else{
foreach ($arr as $str) {
$tmp = explode(" ", trim($str));
foreach ($format as $key => $value) {
$result[$value][] = $tmp[$key];//经测试这个操作比较耗时,大概0.7s的样子,慎用!
}
}
}
return $result;
}
/*
* get file attributes
* @var $file
* @var $flag
* @return string
*/
function getfileattr($file,$flag){
if (!file_exists($file)) {
return false;
}
switch ($flag) {
case 'dir':
if(is_file($file))
return dirname($file);
return realpath($file);
break;
case 'name':
if(is_file($file))
return basename($file);
return '-';
break;
case 'size':
if(is_file($file))
return filesize($file);
return '-';
break;
case 'perms':
return substr(sprintf('%o', fileperms($file)), -4);;
break;
case 'ower':
return fileowner($file);
break;
case 'group':
return filegroup($file);
break;
case 'ctime':
return filectime($file);
break;
case 'mtime':
return filemtime($file);
break;
case 'atime':
return fileatime($file);
break;
case 'suffix':
if(is_file($file))
return substr($file, strrpos($file, '.')+1);
return '-';
break;
default:
return false;
break;
}
}
/*
* get file size human readable
*/
function getfilesizeformat($file){
if(!is_file($file)){
return '-';
}
$flags = array('', 'k', 'm', 'g', 't');
for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
return round($size, 2).$flags[$i];
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
在Win2003(64位)中配置IIS6+PHP5.2.17+MySQL5.5的运行环境
2025-05-29 50 -
2025-05-27 83
-
2025-05-29 37
-
2025-05-29 110
-
2025-05-26 61
热门评论



