文件上传与下载学习笔记(3)---面向对象方法实现文件上传

代码:

  1 <?php
  2 class uploadClass {
  3     protected $filename;
  4     protected $maxSize;
  5     protected $allowExt;
  6     protected $allowMime;
  7     protected $uploadPath;
  8     protected $imgFlag;
  9     protected $fileInfo;
 10     protected $error;
 11     protected $ext;
 12     protected $uniName;
 13     protected $dst;
 14     //构造函数初始化
 15     public function __construct($fileName = "myFile", $uploadPath = "oop", $maxSize = 5242880, $allowExt = array("jpeg","jpg","png","gif"), $allowMime = array("image/jpeg","image/jpg","image/png","image/gif"), $imgFlag = false) {
 16         $this->fileName = $fileName;
 17         $this->fileInfo = $_FILES [$this->fileName];
 18         $this->maxSize = $maxSize;
 19         $this->allowMime = $allowMime;
 20         $this->ext = pathinfo ( $this->fileInfo [‘name‘], PATHINFO_EXTENSION );
 21         $this->allowExt = $allowExt;
 22         $this->uploadPath = $uploadPath;
 23         $this->imgFlag = $imgFlag;
 24         $this->uniName=md5(uniqid(microtime(true),true));
 25         $this->dst=$this->uploadPath.‘/‘.$this->uniName.‘.‘.$this->ext;
 26     }
 27     /**
 28      * 检测上传是否有错
 29      * 
 30      * @return boolean
 31      */
 32     protected function checkError() {
 33         if ($this->fileInfo [‘error‘] == 0) {
 34             return true;
 35         } else {
 36             switch ($this->fileInfo [‘error‘]) {
 37                 case 1 :
 38                     $this->error = $fileInfo [‘name‘] . ‘上传文件超过了upload_max_filesize 选项的值‘;
 39                     break;
 40                 case 2 :
 41                     $this->error = $fileInfo [‘name‘] . ‘超过了表单MAX_FILE_SIZE限制的大小‘;
 42                     break;
 43                 case 3 :
 44                     $this->error = $fileInfo [‘name‘] . ‘文件上传不完整‘;
 45                     break;
 46                 case 4 :
 47                     $this->error = $fileInfo [‘name‘] . ‘没有选择上传文件‘;
 48                     break;
 49                 case 6 :
 50                     $this->error = $fileInfo [‘name‘] . ‘没有找到临时目录‘;
 51                     break;
 52                 case 7 :
 53                     $this->error = $fileInfo [‘name‘] . ‘文件写入失败‘;
 54                     break;
 55                 case 8 :
 56                     $this->error = $fileInfo [‘name‘] . ‘文件上传被php扩展程序中断‘;
 57                     break;
 58             }
 59             return false;
 60         }
 61     }
 62     /**
 63      * 检查文件大小
 64      * 
 65      * @return boolean
 66      */
 67     protected function checkSize() {
 68         if ($this->fileInfo [‘size‘] > $this->maxSize) {
 69             $this->error = "上传文件过大";
 70             return false;
 71         } else {
 72             return true;
 73         }
 74     }
 75     /**
 76      * 检查文件类型
 77      * 
 78      * @return boolean
 79      */
 80     protected function checkExt() {
 81         if (! in_array ( $this->ext, $this->allowExt )) {
 82             $this->error = "不正确的文件";
 83             return false;
 84         } else {
 85             return true;
 86         }
 87     }
 88     /**
 89      * 检测文件的Mime类型
 90      * 
 91      * @return boolean
 92      */
 93     protected function checkMime() {
 94         if (! in_array ( $this->fileInfo [‘type‘], $this->allowMime )) {
 95             $this->error = "文件类型不正确";
 96             return false;
 97         } else {
 98             return true;
 99         }
100     }
101     /**
102      * 检测是否是真实图片
103      * 
104      * @return boolean
105      */
106     protected function checkTureImg() {
107         if ($this->imgFlag) {
108             if (@! getimagesize ( $this->fileInfo [‘tmp_name‘] )) {
109                 $this->error = "不是真正的图片";
110                 return false;
111             } else {
112                 return true;
113             }
114         }else{
115             return true;
116         }
117     }
118     /**
119      * 检测是否通过HTTP POST 上传
120      * @return boolean
121      */
122     protected function checkPost() {
123         if (!is_uploaded_file ( $this->fileInfo [‘tmp_name‘] )) {
124             $this->error = "不是通过 HTTP POST 方式上传";
125             return false;
126         } else {
127             return true;
128         }
129     }
130     /**
131      * 显示错误信息
132      */
133     protected function showError(){
134         exit("<span color=‘red‘>".$this->error."</span>");
135     }
136     /**
137      * 检测文件夹是否存在,不存在则创建
138      */
139     protected function checkPath(){
140         if(!file_exists($this->uploadPath)){
141             mkdir($this->uploadPath,0777,true);
142         }
143     }
144     /**
145      * 文件上传公共接口
146      */
147     public function uploadfile() {
148         if ($this->checkError () && $this->checkSize () && $this->checkExt () && $this->checkMime ()  && $this->checkPost () && $this->checkTureImg()) {
149             $this->checkPath();
150             if(move_uploaded_file($this->fileInfo[‘tmp_name‘], $this->dst)){
151                 return $this->dst;
152             }else{
153                 $this->error="文件移动失败";
154                 $this->showError();
155             }
156         } else {
157             $this->showError ();
158         }
159     }
160 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。