本文实例讲述了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<?php class Test_Upload{ protected $_uploaded = array(); protected $_destination; protected $_max = 1024000; protected $_messages = array(); protected $_permited = array( 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png' ); protected $_renamed = false; /** * * @param mix $path * */ public function __construct($path){ if (!is_dir($path) || !is_writable($path)){ throw new Exception("文件名不可写,或者不是目录!"); } $this->_destination = $path; $this->_uploaded = $_FILES; } /** * 移动文件 * */ public function move(){ $filed = current($this->_uploaded); $isOk = $this->checkError($filed['name'], $filed['error']); //debug ok if ($isOk){ $sizeOk = $this->checkSize($filed['name'], $filed['size']); $typeOk = $this->checkType($filed['name'], $filed['type']); if ($sizeOk && $typeOk){ $success = move_uploaded_file($filed['tmp_name'], $this->_destination.$filed['name']); if ($success){ $this->_messages[] = $filed['name']."文件上传成功"; }else { $this->_messages[] = $filed['name']."文件上传失败"; } } } } /** * 查询messages数组内容 * */ public function getMessages(){ return $this->_messages; } /** * 检测上传的文件大小 * @param mix $string * @param int $size */ public function checkSize($filename, $size){ if ($size == 0){ return false; }else if ($size > $this->_max){ $this->_messages[] = "文件超出上传限制大小".$this->getMaxsize(); return false; }else { return true; } } /** * 检测上传文件的类型 * @param mix $filename * @param mix $type */ protected function checkType($filename, $type){ if (!in_array($type, $this->_permited)){ $this->_messages[] = "该文件类型是不被允许的上传类型"; return false; }else { return true; } } /** * 获取文件大小 * */ public function getMaxsize(){ return number_format($this->_max / 1024, 1).'KB'; } /** * 检测上传错误 * @param mix $filename * @param int $error * */ public function checkError($filename, $error){ switch ($error){ case 0 : return true; case 1 : case 2 : $this->_messages[] = "文件过大!"; return true; case 3 : $this->_messages[] = "错误上传文件!";return false; case 4 : $this->_messages[] = "没有选择文件!"; return false; default : $this->_messages[] = "系统错误!"; return false; } } } ?>
希望本文所述对大家的php程序设计有所帮助。
最后
以上就是繁荣马里奥最近收集整理的关于适用于初学者的简易PHP文件上传类的全部内容,更多相关适用于初学者内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复