-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.func.php
220 lines (196 loc) · 4.46 KB
/
file.func.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
<?php
header("Content-Type:text/html; charset=utf-8");
//Bytes/Kb/MB/GB/TB/EB
/**
* 转换字节大小
* @param number $size
* @return number
*/
function transByte($size) {
$arr = array ("B", "KB", "MB", "GB", "TB", "EB" );
$i = 0;
while ( $size >= 1024 ) {
$size /= 1024;
$i ++;
}
return round ( $size, 2 ) . $arr [$i];
}
/**
* 创建文件
* @param string $filename
* @return string
*/
function createFile($filename) {
//file/1.txt
//验证文件名的合法性,是否包含/,*,<>,?,|
$pattern = "/[\/,\*,<>,\?\|]/";
if (! preg_match ( $pattern, basename ( $filename ) )) {
//检测当前目录下是否存在同名文件
if (! file_exists ( $filename )) {
//通过touch($filename)来创建
if (touch ( $filename )) {
return "文件创建成功";
} else {
return "文件创建失败";
}
} else {
return "文件已存在,请重命名后创建";
}
} else {
return "非法文件名";
}
}
/**
* 重命名文件
* @param string $oldname
* @param string $newname
* @return string
*/
function renameFile($oldname,$newname){
// echo $oldname,$newname;
//验证文件名是否合法
if(checkFilename($newname)){
//检测当前目录下是否存在同名文件
$path=dirname($oldname);
if(!file_exists($path."/".$newname)){
//进行重命名
if(rename($oldname,$path."/".$newname)){
return "重命名成功";
}else{
return "重命名失败";
}
}else{
return "存在同名文件,请重新命名";
}
}else{
return "非法文件名";
}
}
/**
*检测文件名是否合法
* @param string $filename
* @return boolean
*/
function checkFilename($filename){
$pattern = "/[\/,\*,<>,\?\|]/";
if (preg_match ( $pattern, $filename )) {
return false;
}else{
return true;
}
}
/**
* 删除文件
* @param string $filename
* @return string
* 使用unlink函数
*/
function delFile($filename){
if(unlink($filename)){
$mes="文件删除成功";
}else{
$mes="文件删除失败";
}
return $mes;
}
/**
* 下载文件操作
* @param string $filename
*/
function downFile($filename){
// echo $filename;
// exit;
header("content-disposition:attachment;filename=".basename($filename));
header("content-length:".filesize($filename));//文件大小
readfile($filename);
}
/**
* 复制文件
* @param string $filename
* @param string $dstname
* @return string
*/
function copyFile($filename,$dstname){
if(file_exists($dstname)){
if(!file_exists($dstname."/".basename($filename))){
if(copy($filename,$dstname."/".basename($filename))){
$mes="文件复制成功";
}else{
$mes="文件复制失败";
}
}else{
$mes="存在同名文件";
}
}else{
$mes="目标目录不存在";
}
return $mes;
}
function cutFile($filename,$dstname){
if(file_exists($dstname)){
if(!file_exists($dstname."/".basename($filename))){
if(rename($filename,$dstname."/".basename($filename))){
$mes="文件剪切成功";
}else{
$mes="文件剪切失败";
}
}else{
$mes="存在同名文件";
}
}else{
$mes="目标目录不存在";
}
return $mes;
}
/**
* 上传文件
* @param array $fileInfo
* @param string $path
* @param array $allowExt
* @param int $maxSize
* @return string
*/
function uploadFile($fileInfo,$path,$allowExt=array("gif","jpeg","jpg","png","txt","doc"),$maxSize=10485760){
//判断错误号
if($fileInfo['error']==UPLOAD_ERR_OK){
//文件是否是通过HTTP POST方式上传上来的
if(is_uploaded_file($fileInfo['tmp_name'])){
//上传文件的文件名,只允许上传jpeg|jpg、png、gif、txt的文件
//$allowExt=array("gif","jpeg","jpg","png","txt");
$ext=getExt($fileInfo['name']);//取出扩展名
$uniqid=getUniqidName();//产生一个唯一的文件
$destination=$path."/".pathinfo($fileInfo['name'],PATHINFO_FILENAME)."_".$uniqid.".".$ext;
if(in_array($ext,$allowExt)){
if($fileInfo['size']<=$maxSize){
if(move_uploaded_file($fileInfo['tmp_name'], $destination)){
$mes="文件上传成功";
}else{
$mes="文件移动失败";
}
}else{
$mes="文件过大";
}
}else{
$mes="非法文件类型";
}
}else{
$mes="文件不是通过HTTP POST方式上传上来的";
}
}else{
switch($fileInfo['error']){
case 1:
$mes="超过了配置文件的大小";
break;
case 2:
$mes="超过了表单允许接收数据的大小";
break;
case 3:
$mes="文件部分被上传";
break;
case 4:
$mes="没有文件被上传";
break;
}
}
return $mes;
}