-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupload.class.php
More file actions
139 lines (127 loc) · 3.76 KB
/
Copy pathupload.class.php
File metadata and controls
139 lines (127 loc) · 3.76 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
<?php
namespace Uploads;
class upload{
protected $fileName;
protected $uploadPath;
protected $error;
protected $ext;
protected $allowMime;
protected $allowExt;
protected $imgFlag;
protected $fileInfo;
protected $maxSize;
protected $destination;
public function __construct($fileName='myFile',$uploadPath='./uploads',$maxSize=5555555555,$imgFlag=true,$allowExt=array('image/jpg','image/jpeg','image/png','image/gif'),$allowExt=array('jpg','jpeg','gif')){
$this->fileName=$fileName;
$this->uploadPath=$uploadPath;
$this->allowExt=$allowExt;
$this->allowMime=$allowMime;
$this->imgFlag=$imgFlag;
$this->maxSize=$maxSize;
$this->fileInfo=$_FILES[$this->fileName];
}
public function checkError(){
if(!is_null($this->fileInfo)){
if($this->fileInfo['error']>0){
switch($this->fileInfo['error']){
case 1:
$this->error='超过了PHP配置文件中upload_max_filesize选项的值';
break;
case 2:
$this->error='超过了表单中MAX_FILE_SIZE设置的值';
break;
case 3:
$this->error='文件部分被上传';
break;
case 4:
$this->error='没有选择上传文件';
break;
case 6:
$this->error='没有找到临时目录';
break;
case 7:
$this->error='文件不可写';
break;
case 8:
$this->error='由于PHP的扩展程序中断文件上传';
break;
}else{
return true;
}
}
}else{
$this->error='文件上传出错';
return false;
}
}
public function checkSize(){//检查文件大小错误
if($this->fileInfo['size']>$this->maxSize){
$this->error='上传文件过大';
return false;
}else{
return true;
}
}
public function checkExt(){//检查文件拓展名错误
$this->ext=strtolower(PATHINFO($this->fileInfo['name'],PATHINFO_EXTENSION));
if(!in_array($this->ext,$this->allowExt)){
$this->error='文件类型有误';
return false;
}else{
return true;
}
}
public function checkHTTP(){//检查文件是否通过http方式上传
if(!is_uploaded_file($this->fileInfo['tmp_name'])){
$this->error='文件不是通过POST方法上传的';
return false;
}else{
return true;
}
public function checkMime(){//检查文件类型
if(!in_array($this->fileInfo['type'],$this->allowMime)){
$this->error='不允许的文件类型';
return false;
}else{
return true;
}
}
public function checkFile(){//检查文件是不是图片
if($imgFlag){
if(!@getimagesize($this->fileInfo['tmp_name'])){
$this->error='不是文件';
return false;
}else{
return true;
}
}else{
return false;
}
}
public function checkDir(){//检查文件上传文件夹是否在,不在就建立一个
if(!isset($this->uploadPath)){
mkdir($this->uploadPath,0777,ture);
}
}
protected function getUniqName(){//获得上传唯一文件名称
return md5(uniqid(microtime(true)),true);
}
protected function showError(){//报告错误
echo '<script type="text/javascript">alter({$this->error});</script>';
}
public function uploadFile(){//文件上传主方法
if($this->checkError()&&$this->checkSize()&&$this->checkExt()&&$this->checkMime()&&$this->checkFile()&&$this->checkHTTP()){
$this->uploadPath=$this->checkDir();
$this->destination=$this->uploadPath.'/'.$this->getUniqName().'.'.$this->ext;
if(move_uploaded_file($this->fileInfo['tmp_name'],$this->destination)){
return $this->destination;
}else{
$this->error='文件移动失败';
$this->showError();
}
}else{
$this->showError();
}
}
}
?>