-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.func.php
171 lines (159 loc) · 4.37 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
<?php
/**
* Created by PhpStorm.
* User: arno
* Date: 14-12-28
* Time: 下午11:17
*/
//bytes/KB/MB/GB/TB/EB
/**
* 转换字节大小
* @param $size
* @return string
*/
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 $fileName 文件名
* @return string
*/
function createFile($fileName){
if(!isvalidate($fileName)){
if(!file_exists($fileName)){
if(touch($fileName)){
return "文件创建成功";
}else{
return "文件创建失败";
}
}else{
return "文件名已经存在,请重新命名";
}
}else{
return "文件名不能包含非法字符";
}
}
function renameFile($oldName, $newName){
$newName = dirname($oldName)."/".$newName;
if(!file_exists($oldName)){
$msg = "原文件不存在,重命名失败";
}elseif( $oldName ==$newName){
$msg ="文件名不能和原文见名相同";
}elseif(!file_exists($newName)){
if(isvalidate($newName)){
$msg ="文件名不能包含非法字符";
}else if(rename($oldName, $newName)){
$msg = "重命名成功";
}else{
$msg = "重命名失败";
}
}else{
$msg = "文件名已经存在";
}
return $msg;
}
function deleteFile($fileName){
if(file_exists($fileName)){
if(unlink($fileName)){
$msg = "删除成功";
}else{
$msg ="删除失败";
}
}else if(is_dir($fileName)){
}
return $msg;
}
function copyFile($oldFile, $newFIle){
if(file_exists($newFIle)){
if(!file_exists($newFIle."/".basename($oldFile))){
if(copy($oldFile,$newFIle."/".basename($oldFile))){
$msg = "文件复制成功";
}else{
$msg="文件复制失败";
}
}else{
$msg ="该目录下已经存在相同的文件";
}
}else{
$msg ='目标文件夹不存在';
}
return $msg;
}
function downloadFile($fileName){
header("content-disposition:attachment;filename=".basename($fileName));
header("content-length:".filesize($fileName));
readfile($fileName);
}
function uploadFile($file, $path){
$allowSuffix = array('gif', 'jpeg', 'jpg', 'png', 'text', 'php');
$maxSize = 1024*1024*10; //10M
//判断错误号
if($file['error'] ==UPLOAD_ERR_OK){
if(is_uploaded_file($file['tmp_name'])){
$suffix = get_extension($file['name']);
$unipid =getUniqidName();
$destination = $path."/".pathinfo($file['name'],PATHINFO_FILENAME)."_".$unipid.".".$suffix;
if(in_array($suffix,$allowSuffix)){
if($file['size'] <= $maxSize){
if(move_uploaded_file($file['tmp_name'],$destination)){
$msg = "文件上传成功";
}else{
$msg = "文件上传失败";
}
}else{
$msg = "文件大小不能超过10M";
}
}else{
$msg = "非法文件类型";
}
}else{
$msg = "文件不是通过HTTP POST方式上传的";
}
}else{
switch($file['error']){
case 1:
$msg = "超过了配置文件的大小";
break;
case 2:
$msg = "超过了表单允许接收数据的大小";
break;
case 3:
$msg ="文件没有上传完成";
break;
case 4:
$msg ="没有文件被上传";
break;
}
}
return $msg;
}
function cuteFile($src, $dist){
if(!isvalidate($dist)){
if(!file_exists($dist."/".basename($src))){
if(rename($src, $dist."/".basename($src))){
$msg ="剪切成功";
}else{
$msg="剪切失败";
}
}else{
$msg = "已存在相同的文件";
}
}else{
$msg = "不能包含非法字符";
}
return $msg;
}
function isvalidate($param){
$p = '/[\/,\*,<>,\?\|]/';
if(preg_match($p, basename($param))){
return true;
}
return false;
}