file_class.php
6.62 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
<?php if(!defined('DEDEINC')) exit('dedecms');
/**
* 文件管理逻辑类
*
* @version $Id: file_class.php 1 19:09 2010年7月12日Z tianya $
* @package DedeCMS.Administrator
* @copyright Copyright (c) 2007 - 2010, DesDev, Inc.
* @license http://help.dedecms.com/usersguide/license.html
* @link http://www.dedecms.com
*/
class FileManagement
{
var $baseDir="";
var $activeDir="";
//是否允许文件管理器删除目录;
//默认为不允许 0 ,如果希望可能管理整个目录,请把值设为 1 ;
var $allowDeleteDir=0;
//初始化系统
function Init()
{
global $cfg_basedir, $activepath;
$this->baseDir = $cfg_basedir;
$this->activeDir = $activepath;
}
//更改文件名
function RenameFile($oldname,$newname)
{
$oldname = $this->baseDir.$this->activeDir."/".$oldname;
$newname = $this->baseDir.$this->activeDir."/".$newname;
if(($newname!=$oldname) && is_writable($oldname))
{
rename($oldname,$newname);
}
ShowMsg("成功更改一个文件名!","file_manage_main.php?activepath=".$this->activeDir);
return 0;
}
//创建新目录
function NewDir($dirname)
{
$newdir = $dirname;
$dirname = $this->baseDir.$this->activeDir."/".$dirname;
if(is_writable($this->baseDir.$this->activeDir))
{
MkdirAll($dirname,$GLOBALS['cfg_dir_purview']);
CloseFtp();
ShowMsg("成功创建一个新目录!","file_manage_main.php?activepath=".$this->activeDir."/".$newdir);
return 1;
}
else
{
ShowMsg("创建新目录失败,因为这个位置不允许写入!","file_manage_main.php?activepath=".$this->activeDir);
return 0;
}
}
/**
* 移动文件
*
* @access public
* @param string $mfile 文件
* @param string $mpath 路径
* @return string
*/
function MoveFile($mfile, $mpath)
{
if($mpath!="" && !preg_match("#\.\.#", $mpath))
{
$oldfile = $this->baseDir.$this->activeDir."/$mfile";
$mpath = str_replace("\\","/",$mpath);
$mpath = preg_replace("#\/{1,}#", "/", $mpath);
if(!preg_match("#^/#", $mpath))
{
$mpath = $this->activeDir."/".$mpath;
}
$truepath = $this->baseDir.$mpath;
if(is_readable($oldfile) && is_readable($truepath) && is_writable($truepath))
{
if(is_dir($truepath))
{
copy($oldfile, $truepath."/$mfile");
}
else
{
MkdirAll($truepath, $GLOBALS['cfg_dir_purview']);
CloseFtp();
copy($oldfile,$truepath."/$mfile");
}
unlink($oldfile);
ShowMsg("成功移动文件!","file_manage_main.php?activepath=$mpath",0,1000);
return 1;
}
else
{
ShowMsg("移动文件 $oldfile -> $truepath/$mfile 失败,可能是某个位置权限不足!","file_manage_main.php?activepath=$mpath",0,1000);
return 0;
}
}
else
{
ShowMsg("对不起,你移动的路径不合法!","-1",0,5000);
return 0;
}
}
/**
* 删除目录
*
* @param unknown_type $indir
*/
function RmDirFiles($indir)
{
if(!is_dir($indir))
{
return ;
}
$dh = dir($indir);
while($filename = $dh->read())
{
if($filename == "." || $filename == "..")
{
continue;
}
else if(is_file("$indir/$filename"))
{
@unlink("$indir/$filename");
}
else
{
$this->RmDirFiles("$indir/$filename");
}
}
$dh->close();
@rmdir($indir);
}
/**
* 获得某目录合符规则的文件
*
* @param unknown_type $indir
* @param unknown_type $fileexp
* @param unknown_type $filearr
*/
function GetMatchFiles($indir, $fileexp, &$filearr)
{
$dh = dir($indir);
while($filename = $dh->read())
{
$truefile = $indir.'/'.$filename;
if($filename == "." || $filename == "..")
{
continue;
}
else if(is_dir($truefile))
{
$this->GetMatchFiles($truefile, $fileexp, $filearr);
}
else if(preg_match("/\.(".$fileexp.")/i",$filename))
{
$filearr[] = $truefile;
}
}
$dh->close();
}
/**
* 删除文件
*
* @param unknown_type $filename
* @return unknown
*/
function DeleteFile($filename)
{
$filename = $this->baseDir.$this->activeDir."/$filename";
if(is_file($filename))
{
@unlink($filename); $t="文件";
}
else
{
$t = "目录";
if($this->allowDeleteDir==1)
{
$this->RmDirFiles($filename);
} else
{
// 完善用户体验,by:sumic
ShowMsg("系统禁止删除".$t."!","file_manage_main.php?activepath=".$this->activeDir);
exit;
}
}
ShowMsg("成功删除一个".$t."!","file_manage_main.php?activepath=".$this->activeDir);
return 0;
}
}
//目录文件大小检测类
class SpaceUse
{
var $totalsize=0;
function checksize($indir)
{
$dh=dir($indir);
while($filename=$dh->read())
{
if(!preg_match("#^\.#", $filename))
{
if(is_dir("$indir/$filename"))
{
$this->checksize("$indir/$filename");
}
else
{
$this->totalsize=$this->totalsize + filesize("$indir/$filename");
}
}
}
}
function setkb($size)
{
$size=$size/1024;
if($size>0)
{
list($t1,$t2)=explode(".",$size);
$size=$t1.".".substr($t2,0,1);
}
return $size;
}
function setmb($size)
{
$size=$size/1024/1024;
if($size>0)
{
list($t1,$t2)=explode(".",$size);
$size=$t1.".".substr($t2,0,2);
}
return $size;
}
}