php上传类

  1 <?php
  2 $upload = new UpLoad();
  3 $upload->uploadFile('fm');
  4 /*打印错误信息*/
  5 // var_dump($upload->errorNumber);
  6 // var_dump($upload->errorInfo);
  7 class UpLoad{
  8     //文件上传路径
  9     protected $path = 'upload/';
 10     //允许文件上传的后缀
 11     protected $allowSuffix = ['jpg','jpeg',
 12     'gif','wbmp','png'];
 13     //mime类型 
 14     protected  $allowMime =['image/jpg','image/jpeg',
 15     'image/gif','image/wbmp','image/png'];
 16     //允许上传的大小
 17     protected $maxSize = 2000000;
 18     //是否启用默认的前缀
 19     protected $isRandName =true ;
 20     //文件的前缀
 21     protected $prefix = 'up_';
 22     //错误号和错误信息
 23     protected  $errorNumber;
 24     protected  $errorInfo;
 25     //文件的信息
 26     //文件名
 27     protected $oldName;
 28     //文件的后缀
 29     protected $suffix;
 30     //文件的大小
 31     protected $size;
 32     //文件的mime
 33     protected $mime;
 34     //文件的临时文件的路径
 35     protected $tmpName;
 36     //文件新名字
 37     protected $newName;
 38     
 39     //构造方法
 40     //因为成员属性比较多就用数组来显示
 41     public function __construct($arr =[]){
 42         foreach ($arr as $key=>$value){
 43             $this->setOption($key,$value);
 44         }
 45     }
 46     //判断$key是不是我的成员属性,如果是就设置
 47     protected function setOption($key,$value){
 48         //得到所有的成员属性
 49         $keys = array_keys(get_class_vars(__CLASS__));
 50         if(in_array($key, $keys)){
 51             $this->$key = $value;
 52         }
 53     }
 54     //文件上传函数
 55     //key 就是input框中的name属性值
 56     public function uploadFile($key){
 57         //判断有没有设置路径 path
 58         if(empty($this->path)){
 59             $this->setOption('errorNumber',-1 );
 60             return false;
 61         }
 62         //判断该路径是否存在是否可写
 63         if (!$this->check()){
 64             $this->setOption('errorNumber', -2);
 65             return false; 
 66         }
 67         //判断$_FILES里面的error信息是否为0,如果为0则说明文件信息在服务器端可以直接获取,提取信息保存到成员属性中
 68         $error = $_FILES[$key]['error'];
 69         if($error){
 70             $this->setOption('errorNumber', -3);
 71             return false;
 72         }else {
 73             //提取文件相关信息并且保存到成员属性中
 74             $this->getFileInfo($key);
 75         }
 76         //判断文件的大小、mime、后缀是否符合
 77          if(!$this->checkSize() || !$this->checkMime()|| !$this->checkSuffix()){
 78              return false;
 79          }
 80         //得到新的文件名字
 81         $this->newName = $this->createNewName();
 82         //判断是否是上传文件,并且是移动上传文件
 83         if(is_uploaded_file($this->tmpName)){
 84             if(move_uploaded_file($this->tmpName, $this->path.$this->newName)){
 85                 return $this->path.$this->newName;
 86             }else {
 87                 $this->setOption('errorNumber', -7);
 88                 return false;
 89             }
 90         }else{
 91             $this->setOption('errorNumber', -6);
 92             return false;
 93         }
 94     }
 95     //检测文件夹是否存在,是否可写
 96     protected function check(){
 97         //文件夹不存在或者不是目录。创建文件夹
 98         if(!file_exists($this->path) ||!is_dir($this->path)){
 99             return mkdir($this->path,0777,true);
100         }
101         //判断文件是否可写
102         if(!is_writeable($this->path)){
103             return chmod($this->path, 0777);
104         }
105         return true;
106     }
107     //根据key得到文件信息
108     protected function getFileInfo($key){
109         //得到文件的名字
110         $this->oldName = $_FILES[$key]['name'];
111         //得到文件的mime类型
112         $this->mime = $_FILES[$key]['type'];
113         //得到文件的临时文件
114         $this->tmpName = $_FILES[$key]['tmp_name'];
115         //得到文件大小
116         $this->size = $_FILES[$key]['size'];
117         //得到文件后缀
118         $this->suffix = pathinfo($this->oldName)['extension'];
119     }
120     //判断文件大小
121     protected function checkSize(){
122         if($this->size > $this->maxSize){
123             $this->setOption('errorNumber', -3);
124             return false;
125         }
126         return true;
127     }
128     //判断mime类型
129     protected function checkMime(){
130         if(!in_array($this->mime, $this->allowMime)){
131             $this->setOption('errorNumber', -4);
132             return false;
133         }
134         return true;
135     }
136     //判断后缀
137     protected function checkSuffix(){
138         if(!in_array($this->suffix, $this->allowSuffix)){
139             $this->setOption('errorNumber', -5);
140             return false;
141         }
142         return true;
143     }
144     //创建新名字
145     protected function createNewName(){
146         if($this->isRandName){
147             $name = $this->prefix.uniqid().'.'.$this->suffix;
148         }else {
149             $name = $this->prefix.$this->oldName;
150         }
151         return $name;
152     }
153     public function __get($name){
154         if($name == 'errorNumber'){
155             return $this->errorNumber;
156         }elseif ($name == 'errorInfo'){
157             return $this->getErrorInfo();
158         }
159     }
160     protected function getErrorInfo(){
161         switch ($this->errorNumber){
162         case -1:
163             $str = '文件路径没有设置';
164             break;
165         case -2:
166             $str = '文件不是目录或者不可写';
167             break;
168         case -3:
169             $str = '文件超过指定大小';
170             break;
171         case -4:
172             $str = 'mime类型不符合';
173             break;
174         case -5:
175             $str = '文件后缀不符合';
176             break;
177         case -6:
178             $str = '不是上传文件';
179             break;
180         case -7:
181             $str = '移动失败';
182             break;
183         case 1:
184             $str = '超出ini设置大小';
185             break;
186         case 2:
187             $str = '超出html表单大小';
188             break;
189         case 3:
190             $str = '文章只有部分上传';
191             break;
192         case 4:
193             $str = '没有文件上传';
194             break;
195         case 6:
196             $str = '找不到临时文件';
197             break;
198         case 7:
199             $str = '文件写入失败';
200             break;
201         }
202         return $str;
203     }
204 }

 

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>文件上传</title>
</head>
<body>
<form action="UpLoad.php" method="post" enctype="multipart/form-data" >
     <input type="file" name="fm" value=""><br>
    <input type="submit" value="上传文件" /><br>
  
</form>
</body>
</html>

注意:input中的name必须和上传类中的uploadFile中是传值一致!

posted @ 2017-07-05 17:09  东东东蔚  阅读(163)  评论(0编辑  收藏  举报