uploadfile_class.php
7.65 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
<?php
/**
* Tiny - A PHP Framework For Web Artisans
* @author Tiny <tinylofty@gmail.com>
* @copyright Copyright(c) 2010-2014 http://www.tinyrise.com All rights reserved
* @version 1.0
*/
/**
* 文件uploadFile类
*
* @author Tiny
* @class UploadFile
*/
class UploadFile extends File
{
const SUCCESS = 1;
const FAILSIZE = 2;
const FAILTYPE = 3;
private $types=',jpg,png,gif,jpeg,rar,zip,';
private $size;
private $uploadPath;
private $filename;
private $files=null;
private $fileNameId = '';//唯一名字上传时
private $info;
private $nameType;
/**
* 构造初始化
*
* @access public
* @param mixed $fieldName
* @param string $uploadPath
* @param string $size
* @param string $types
* @param string $nameType
* @param string $nameId
*/
public function __construct($fieldName, $uploadPath="", $size='500k', $types='', $nameType='date',$nameId='')
{
if(isset($_FILES[$fieldName]))
{
$this->files = $_FILES[$fieldName];
if($uploadPath=='')$this->uploadPath = Tiny::getPath('uploads');
else $this->uploadPath = $uploadPath;
$endchar=strtolower(substr($size,-1));
if($endchar=='k') $this->size = substr($size,0,-1) << 10;
else if($endchar=='m') $this->size = substr($size,0,-1) << 20;
else $this->size = intval($size);
if($types!='')$this->types=",$types,";
$this->nameType = $nameType;
$this->fileNameId = $nameId;
}
}
/**
* 保存
*
* @access public
* @return mixed
*/
public function save()
{
if(!is_null($this->files))
{
if(is_array($this->files['name']))
{
$num = count($this->files['name']);
for($i=0;$i<$num;$i++)
{
if($this->checkSize($this->files['size'][$i]) && $this->checkType($this->files['name'][$i]))
{
$fileName = md5_file($this->files['tmp_name'][$i]).$this->files['name'][$i];
$realpath=$this->getRealPath($fileName);
if(!file_exists(dirname($realpath)))$this->mkdir($this->getUploadPath().dirname($realpath));
move_uploaded_file($this->files['tmp_name'][$i],$this->getUploadPath().$realpath);
$this->info[]=array('name'=>$this->files['name'][$i],'path'=>$realpath,'size'=>$this->files['size'][$i],'type'=>$this->files['type'][$i],'status'=>self::SUCCESS,'msg'=>'上传成功');
}
else if(!$this->checkSize($this->files['size'][$i]))
{
$this->info[]=array('name'=>$this->files['name'][$i],'path'=>'','size'=>$this->files['size'][$i],'type'=>$this->files['type'][$i],'status'=>self::FAILSIZE,'msg'=>'超出了文件规定的大小');
}
else if(!$this->checkType($this->files['name'][$i]))
{
$this->info[]=array('name'=>$this->files['name'][$i],'path'=>'','size'=>$this->files['size'][$i],'type'=>$this->files['type'][$i],'status'=>self::FAILTYPE,'msg'=>'文件上传的类型不符合');
}
}
}
else if(is_string($this->files['name']))
{
if($this->checkSize($this->files['size']) && $this->checkType($this->files['name']))
{
$fileName = md5_file($this->files['tmp_name']).$this->files['name'];
$realpath = $this->getRealPath($fileName);
if(!file_exists(dirname($realpath)))$this->mkdir($this->getUploadPath().dirname($realpath));
$this->info[]=array('name'=>$this->files['name'],'path'=>$realpath,'size'=>$this->files['size'],'type'=>$this->files['type'],'status'=>self::SUCCESS,'msg'=>'上传成功');
move_uploaded_file($this->files['tmp_name'],$this->getUploadPath().$realpath);
}
else if(!$this->checkSize($this->files['size']))
{
$this->info[]=array('name'=>$this->files['name'],'path'=>'','size'=>$this->files['size'],'type'=>$this->files['type'],'status'=>self::FAILSIZE,'msg'=>'超出了文件规定的大小');
}
else if(!$this->checkType($this->files['name']))
{
$this->info[]=array('name'=>$this->files['name'],'path'=>'','size'=>$this->files['size'],'type'=>$this->files['type'],'status'=>self::FAILTYPE,'msg'=>'文件上传的类型不符合');
}
}
}
}
/**
* 设置保存目录
*
* @access public
* @param mixed $uploadPath
* @return mixed
*/
public function setUploadPath($uploadPath)
{
$this->uploadPath=$uploadPath;
}
/**
* 得到上传目录
*
* @access public
* @return mixed
*/
public function getUploadPath()
{
return $this->uploadPath;
}
/**
* 设置允许的大小
*
* @access public
* @param mixed $size
* @return mixed
*/
public function setSize($size)
{
$this->size=$size;
}
/**
* 取得大小
*
* @access public
* @return mixed
*/
public function getSize()
{
$this->size;
}
/**
* 设置类型
*
* @access public
* @param mixed $types
* @return mixed
*/
public function setType($types)
{
$this->types=$types;
}
/**
* 得到类型
*
* @access public
* @return mixed
*/
public function getType()
{
return $this->types;
}
/**
* 取得上传后的返回信息
*
* @access public
* @return mixed
*/
public function getInfo()
{
return $this->info;
}
/**
* 检测大小
*
* @access public
* @param mixed $size
* @return mixed
*/
public function checkSize($size)
{
return ($size<=$this->size && $size>0);
}
/**
* 检测类型
*
* @access public
* @param mixed $name
* @return mixed
*/
public function checkType($name)
{
$fileName_array = explode(".",strtolower($name));
return (strpos($this->types,end($fileName_array)));
}
/**
* 取得文件
*
* @access public
* @return mixed
*/
public function getFiles()
{
return $this->files;
}
/**
* 得到文件有真实路径
*
* @access private
* @param mixed $name
* @return mixed
*/
private function getRealPath($name)
{
$fileName_array = explode(".",strtolower($name));
$ext=end($fileName_array);
if($this->nameType == 'date')
{
$date = new Date();
return $date->format('Y/m/d/').Crypt::md5($name).'.'.$ext;
}
else if($this->nameType == 'hash')
{
if($this->fileNameId!='')$num=crc32($this->fileNameId);
else $num=crc32($name.time());
$num = sprintf('%u',$num);
$index=($num%1024)."/".(($num>>=10)%1024)."/".($num>>=10)."/".Crypt::md5($this->fileNameId).'.'.$ext;
return $index;
}
else
{
$date = new Date();
return $date->format('Y/m/d/').$name;
}
}
}