-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFlamez57Upload.php
More file actions
436 lines (405 loc) · 9.43 KB
/
Flamez57Upload.php
File metadata and controls
436 lines (405 loc) · 9.43 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<?php
namespace Flamez57;
/*
** Flamez57Upload使用说明:
** ===========================================================
** 实例化文件上传器
** $upload = new \Flamez57\Flamez57Upload(['filepath' => $filepath, '$allowtype' => array('gif', 'jpg', 'png', 'jpeg'), 'maxsize' => '1000000', 'israndname' => true]);
** ===========================================================
*/
class Flamez57Upload
{
/*
** 指定上传文件保存的路径
*/
private $filepath;
/*
** 充许上传文件的类型
*/
private $allowtype=array('gif', 'jpg', 'png', 'jpeg');
/*
** 允上传文件的最大长度 1M
*/
private $maxsize=1000000;
/*
** 是否随机重命名, false不随机,使用原文件名
*/
private $israndname=true;
/*
** 是否覆盖同名文件
*/
private $over_write=false;
/*
** 源文件名称
*/
private $originName;
/*
** 临时文件名
*/
private $tmpFileName;
/*
** 文件类型
*/
private $fileType;
/*
** 文件大小
*/
private $fileSize;
/*
** 新文件名
*/
private $newFileName;
/*
** 错误号
*/
private $errorNum=0;
/*
** 用来提供错误报告
*/
private $errorMess="";
/*
** 用于对上传文件初使化
** $options = [
** 'filepath' => $filepath, 指定上传路径
** '$allowtype' => array('gif', 'jpg', 'png', 'jpeg'), 充许的类型
** 'maxsize' => '1000000', 限制大小
** 'israndname' => true 是否使用随机文件名称
** ];
*/
public function __construct($options=array())
{
foreach($options as $key=>$val){
$key=strtolower($key);
//查看用户参数中数组的下标是否和成员属性名相同
if(!in_array($key,get_class_vars(get_class($this)))){
continue;
}
$this->setOption($key, $val);
}
}
/*
** 为单个成员属性设置值
*/
private function setOption($key, $val)
{
$this->$key = $val;
}
/*
** 定义错误信息
*/
private function getError()
{
$str="上传文件<font color='red'>{$this->originName}</font>时出错:";
switch($this->errorNum){
case 4:
$str .= "没有文件被上传";
break;
case 3:
$str .= "文件只被部分上传";
break;
case 2:
$str .= "上传文件超过了HTML表单中MAX_FILE_SIZE选项指定的值";
break;
case 1:
$str .= "上传文件超过了php.ini 中upload_max_filesize选项的值";
break;
case -1:
$str .= "末充许的类型";
break;
case -2:
$str .= "文件过大,上传文件不能超过{$this->maxSize}个字节";
break;
case -3:
$str .= "上传失败";
break;
case -4:
$str .= "建立存放上传文件目录失败,请重新指定上传目录";
break;
case -5:
$str .= "必须指定上传文件的路径";
break;
case -6:
$str .= "同名文件已存在!";
break;
case -7:
$str .= "文件不存在";
break;
default:
$str .= "末知错误";
}
return $str.'<br>';
}
/*
** 用来检查文件上传路径
*/
private function checkFilePath()
{
if(empty($this->filepath)) {
$this->setOption('errorNum', -5);
return false;
}
if(!file_exists($this->filepath) || !is_writable($this->filepath)){
if(chmod($this->filepath, 0755)){
$this->setOption('errorNum', -4);
return false;
}
}
return true;
}
/*
** 用来检查文件上传的大小
*/
private function checkFileSize()
{
if($this->fileSize > $this->maxsize){
$this->setOPtion('errorNum', '-2');
return false;
}else{
return true;
}
}
/*
** 用于检查文件上传类型
*/
private function checkFileType()
{
if(in_array(strtolower($this->fileType), $this->allowtype)) {
return true;
}else{
$this->setOption('errorNum', -1);
return false;
}
}
/*
** 设置上传后的文件名称
*/
private function setNewFileName()
{
if($this->israndname){
$this->setOption('newFileName', $this->proRandName());
} else {
$this->setOption('newFileName', $this->originName);
}
}
/*
** 设置随机文件名称
*/
private function proRandName()
{
$fileName=date("YmdHis").rand(100,999);
return $fileName.'.'.$this->fileType;
}
/*
** 用来上传一个文件
*/
public function uploadFile($fileField)
{
$return=true;
//检查文件上传路径
if(!$this->checkFilePath()){
$this->errorMess=$this->getError();
return false;
}
$name=$_FILES[$fileField]['name'];
$tmp_name=$_FILES[$fileField]['tmp_name'];
$size=$_FILES[$fileField]['size'];
$error=$_FILES[$fileField]['error'];
if(is_Array($name)){
$errors=array();
for($i=0; $i<count($name); $i++){
if($this->setFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i])){
if(!$this->checkFileSize() || !$this->checkFileType()){
$errors[]=$this->getError();
$return=false;
}
}else{
$error[]=$this->getError();
$return=false;
}
if(!$return)
$this->setFiles();
}
if($return){
$fileNames=array();
for($i=0; $i<count($name); $i++){
if($this->setFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i])){
$this->setNewFileName();
if(!$this->copyFile()){
$errors=$this->getError();
$return=false;
}else{
$fileNames[]=$this->newFileName;
}
}
}
$this->newFileName=$fileNames;
}
$this->errorMess=$errors;
return $return;
} else {
if($this->setFiles($name, $tmp_name, $size, $error)){
if($this->checkFileSize() && $this->checkFileType()){
$this->setNewFileName();
if($this->copyFile()){
return true;
}else{
$return=false;
}
}else{
$return=false;
}
}else{
$return=false;
}
if(!$return)
$this->errorMess=$this->getError();
return $return;
}
}
/*
** 复制上传文件到指定的位置
*/
private function copyFile()
{
if(!$this->errorNum){
$filepath=rtrim($this->filepath, '/').'/';
$filepath.=$this->newFileName;
if(@move_uploaded_file($this->tmpFileName, $filepath)) {
return true;
}else{
$this->setOption('errorNum', -3);
return false;
}
}else{
return false;
}
}
/*
** 设置和$_FILES有关的内容
*/
private function setFiles($name="", $tmp_name='', $size=0, $error=0)
{
$this->setOption('errorNum', $error);
if($error){
return false;
}
$this->setOption('originName', $name);
$this->setOption('tmpFileName', $tmp_name);
$arrStr=explode('.', $name);
$this->setOption('fileType', strtolower($arrStr[count($arrStr)-1]));
$this->setOption('fileSize', $size);
return true;
}
/*
** 检查是否有同名文件,是否覆盖
*/
private function check_same_file($filename)
{
if(file_exists($filename)&&$this->over_write!=true){
$this->setOption('errorNum', -6);
return false;
}
}
/*
** 检查文件是否是通过 HTTP POST 上传的
*/
private function is_upload_file($tmp_name)
{
if(!is_uploaded_file($tmp_name)){
$this->setOption('errorNum', -7);
return false;
}
}
/*
** 用于获取上传后文件的文件名
*/
public function getNewFileName()
{
return $this->newFileName;
}
/*
** 上传如果失败,则调用这个方法,就可以查看错误报告
*/
public function getErrorMsg()
{
return $this->errorMess;
}
/*
** 生成缩略图
** @param $img_w 新图宽
** @param $img_h 新图高
** @param $filesName 图片文件
** @return $new_name 生成的图片地址
** 最大宽:120,高:120
*/
public function create_simg($img_w,$img_h, $filesName)
{
$new_name="/s_".$filesName;
$imgsize=getimagesize($filesName);
switch ($imgsize[2]) {
case 1:
if(!function_exists("imagecreatefromgif")){
echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!返回";
exit();
}
$im = imagecreatefromgif($filesName);
break;
case 2:
if(!function_exists("imagecreatefromjpeg")){
echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!返回";
exit();
}
$im = imagecreatefromjpeg($filesName);
break;
case 3:
if(!function_exists("imagecreatefrompng")){
echo "你的GD库不能使用png格式的图片,请使用其它格式的图片!返回";
exit();
}
$im = imagecreatefrompng($filesName);
break;
case 4:
if(!function_exists("imagecreatefromwbmp")){
echo "你的GD库不能使用bmp格式的图片,请使用其它格式的图片!返回";
exit();
}
$im = imagecreatefromwbmp($filesName);
break;
default:
die("is not filetype right");
exit;
}
$src_w=imagesx($im);//获得图像宽度
$src_h=imagesy($im);//获得图像高度
$new_wh=($img_w/$img_h);//新图像宽与高的比值
$src_wh=($src_w/$src_h);//原图像宽与高的比值
if($new_wh<=$src_wh){
$f_w=$img_w;
$f_h=$f_w*($src_h/$src_w);
}else{
$f_h=$img_h;
$f_w=$f_h*($src_w/$src_h);
}
if($src_w>$img_w||$src_h>$img_h){
if(function_exists("imagecreatetruecolor")){//检查函数是否已定义
$new_img=imagecreatetruecolor($f_w,$f_h);
if($new_img){
imagecopyresampled($new_img,$im,0,0,0,0,$f_w,$f_h,$src_w,$src_h);//重采样拷贝部分图像并调整大小
}else{
$new_img=imagecreate($f_w,$f_h);
imagecopyresized($new_img,$im,0,0,0,0,$f_w,$f_h,$src_w,$src_h);
}
}else{
$$new_img=imagecreate($f_w,$f_h);
imagecopyresized($new_img,$im,0,0,0,0,$f_w,$f_h,$src_w,$src_h);
}
if(function_exists('imagejpeg')){
imagejpeg($new_img,$new_name);
}else{
imagepng($new_img,$new_name);
}
imagedestroy($new_img);
}
return $new_name;
}
}