<?php /** *本类为文件操作类,实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录 * 列出目录里的文件等功能,路径后面别忘了加"/" * * @author 路人郝 * @copyright myself * @link [url=http://www.phpr.cn]www.phpr.cn[/url] * */ class fileoperate { var $path;// 文件路径 var $name;//文件名 var $result;//对文件操作后的结果 /** * 本方法用来在path目录下创建name文件 * * @param string path * @param string name */ function creat_file($path,$name)//建立文件 { $filename=$path.$name; if (file_exists($filename)) { echo "文件已经存在,请换个文件名"; } else { if (file_exists($path)) { touch($name); rename($name,$filename); echo "文件建立成功 </br>"; } else{ echo "目录不存在,请检查"; } } } /** * 本方法删除path路径下name文件 * * @param string_type path * @param string_type name */ function del_file($path,$name){ //删除文件 $filename=$path.$name; if (!file_exists($filename)) { echo "文件不存在,请确认路径是否正确"; } else { if (unlink($filename)){ echo "文件删除成功"; } else echo "文件删除失败"; } } /** * 本方法把filename文件重命名为newname文件 * * @param string_type filename * @param string_type newname */ function rename_file($filename,$newname) { //文件或目录更名 $path=pathinfo($filename); $dir=$path['dirname']; //得到文件路径 $newfilename=$dir.'/'.$newname; if (file_exists($filename)) { //判断文件是否存在 $result=rename($filename,$newfilename); if ($result==true) echo "文件更名成功"; else echo "文件更名失败"; } else echo "文件不存在"; } /** * 本方法用来列出目录里的文件或目录switch为1时按字母顺序列出所有目录和文件 * switch为2则只列出目录,switch为3时,只列出文件名 * * @param string_type path * @param int_type switch */ function list_filename($path,$switch) //列出文件和目录名 { if (file_exists($path)) { $dir=scandir($path); if ($switch==1){ //如果switch为1则按字母顺序列出所有目录和文件 for ($i=0;$i<=count($dir);$i++) { if ($dir[$i]!="." and $dir[$i]!="..") { echo "$dir[$i]<br>"; } } } if ($switch==2) //switch为2则只列出目录 { for ($i=0;$i<=count($dir);$i++) { $x=is_dir($path.$dir[$i]); if ($dir[$i]!="." and $dir[$i]!=".." and $x==true) { echo "$dir[$i]<br>"; } } } if ($switch==3) //switch为3时,只列出文件名 { for ($i=0;$i<=count($dir);$i++) { $x=is_dir($path.$dir[$i]); if ($dir[$i]!="." and $dir[$i]!=".." and $x==false) { echo "$dir[$i]<br>"; } } } } } /** * 本方法在path目录下创建名为dirname的目录 * * @param string_type path * @param string_type dirname */ function creat_dir($path,$dirname){ //创建目录 if (file_exists($path)) { $result=@mkdir($path.$dirname); if ($result) echo "目录建立成功"; else echo "目录建立失败"; } else echo "路径不存在,请重新输入"; } /** * 本方法删除pathname目录,包括该目录下所有的文件及子目录 * * @param string_type pathname */ function del_dir($pathname){ //删除目录及目录里所有的文件夹和文件 if (!is_dir($pathname)) {exit("你输入的不是一个目录,请检查") ;} $handle=opendir($pathname); while (($fileordir=readdir($handle)) !== false) { if ($fileordir!="." && $fileordir!="..") { is_dir("$pathname/$fileordir")? $this->del_dir("$pathname/$fileordir"): unlink("$pathname/$fileordir"); } } if (readdir($handle) == false) { closedir($handle); rmdir($pathname); } } } ?> <?php $op=new fileoperate(); $op->creat_file("user-files/","aa3.txt"); //$op->del_file("user-files/","aa3.txt"); //$op->rename_file("user-files/aa3.txt","测试文件.txt"); echo '<br>'; $op->list_filename("user-files/",1); $op->creat_dir("user-files/","新建的目录"); $op->del_dir("user-files/新建的目录"); ?>
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
|
<?php
/**
*本类为文件操作类,实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录
* 列出目录里的文件等功能,路径后面别忘了加"/"
*
* @author 路人郝
* @copyright myself
* @link [url=http://www.phpr.cn]www.phpr.cn[/url]
*
*/
class
fileoperate
{
var
$path
;
// 文件路径
var
$name
;
//文件名
var
$result
;
//对文件操作后的结果
/**
* 本方法用来在path目录下创建name文件
*
* @param string path
* @param string name
*/
function
creat_file
(
$path
,
$name
)
//建立文件
{
$filename
=
$path
.
$name
;
if
(
file_exists
(
$filename
)
)
{
echo
"文件已经存在,请换个文件名"
;
}
else
{
if
(
file_exists
(
$path
)
)
{
touch
(
$name
)
;
rename
(
$name
,
$filename
)
;
echo
"文件建立成功 </br>"
;
}
else
{
echo
"目录不存在,请检查"
;
}
}
}
/**
* 本方法删除path路径下name文件
*
* @param string_type path
* @param string_type name
*/
function
del_file
(
$path
,
$name
)
{
//删除文件
$filename
=
$path
.
$name
;
if
(
!
file_exists
(
$filename
)
)
{
echo
"文件不存在,请确认路径是否正确"
;
}
else
{
if
(
unlink
(
$filename
)
)
{
echo
"文件删除成功"
;
}
else
echo
"文件删除失败"
;
}
}
/**
* 本方法把filename文件重命名为newname文件
*
* @param string_type filename
* @param string_type newname
*/
function
rename_file
(
$filename
,
$newname
)
{
//文件或目录更名
$path
=
pathinfo
(
$filename
)
;
$dir
=
$path
[
'dirname'
]
;
//得到文件路径
$newfilename
=
$dir
.
'/'
.
$newname
;
if
(
file_exists
(
$filename
)
)
{
//判断文件是否存在
$result
=
rename
(
$filename
,
$newfilename
)
;
if
(
$result
==
true
)
echo
"文件更名成功"
;
else
echo
"文件更名失败"
;
}
else
echo
"文件不存在"
;
}
/**
* 本方法用来列出目录里的文件或目录switch为1时按字母顺序列出所有目录和文件
* switch为2则只列出目录,switch为3时,只列出文件名
*
* @param string_type path
* @param int_type switch
*/
function
list_filename
(
$path
,
$switch
)
//列出文件和目录名
{
if
(
file_exists
(
$path
)
)
{
$dir
=
scandir
(
$path
)
;
if
(
$switch
==
1
)
{
//如果switch为1则按字母顺序列出所有目录和文件
for
(
$i
=
0
;
$i
<=
count
(
$dir
)
;
$i
++
)
{
if
(
$dir
[
$i
]
!=
"."
and
$dir
[
$i
]
!=
".."
)
{
echo
"$dir[$i]<br>"
;
}
}
}
if
(
$switch
==
2
)
//switch为2则只列出目录
{
for
(
$i
=
0
;
$i
<=
count
(
$dir
)
;
$i
++
)
{
$x
=
is_dir
(
$path
.
$dir
[
$i
]
)
;
if
(
$dir
[
$i
]
!=
"."
and
$dir
[
$i
]
!=
".."
and
$x
==
true
)
{
echo
"$dir[$i]<br>"
;
}
}
}
if
(
$switch
==
3
)
//switch为3时,只列出文件名
{
for
(
$i
=
0
;
$i
<=
count
(
$dir
)
;
$i
++
)
{
$x
=
is_dir
(
$path
.
$dir
[
$i
]
)
;
if
(
$dir
[
$i
]
!=
"."
and
$dir
[
$i
]
!=
".."
and
$x
==
false
)
{
echo
"$dir[$i]<br>"
;
}
}
}
}
}
/**
* 本方法在path目录下创建名为dirname的目录
*
* @param string_type path
* @param string_type dirname
*/
function
creat_dir
(
$path
,
$dirname
)
{
//创建目录
if
(
file_exists
(
$path
)
)
{
$result
=
@
mkdir
(
$path
.
$dirname
)
;
if
(
$result
)
echo
"目录建立成功"
;
else
echo
"目录建立失败"
;
}
else
echo
"路径不存在,请重新输入"
;
}
/**
* 本方法删除pathname目录,包括该目录下所有的文件及子目录
*
* @param string_type pathname
*/
function
del_dir
(
$pathname
)
{
//删除目录及目录里所有的文件夹和文件
if
(
!
is_dir
(
$pathname
)
)
{
exit
(
"你输入的不是一个目录,请检查"
)
;
}
$handle
=
opendir
(
$pathname
)
;
while
(
(
$fileordir
=
readdir
(
$handle
)
)
!==
false
)
{
if
(
$fileordir
!=
"."
&&
$fileordir
!=
".."
)
{
is_dir
(
"$pathname/$fileordir"
)
?
$this
->
del_dir
(
"$pathname/$fileordir"
)
:
unlink
(
"$pathname/$fileordir"
)
;
}
}
if
(
readdir
(
$handle
)
==
false
)
{
closedir
(
$handle
)
;
rmdir
(
$pathname
)
;
}
}
}
?>
<?php
$op
=
new
fileoperate
(
)
;
$op
->
creat_file
(
"user-files/"
,
"aa3.txt"
)
;
//$op->del_file("user-files/","aa3.txt");
//$op->rename_file("user-files/aa3.txt","测试文件.txt");
echo
'<br>'
;
$op
->
list_filename
(
"user-files/"
,
1
)
;
$op
->
creat_dir
(
"user-files/"
,
"新建的目录"
)
;
$op
->
del_dir
(
"user-files/新建的目录"
)
;
?>
|