欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

自定义MVC框架之工具类-文件上传类

程序员文章站 2022-05-03 09:17:53
截止目前已经改造了3个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 该文件上传类功能如下: 1,允许定制上传的文件类型,文件mime信息,文件大小 2,*定制文件名是随机还是保持原来的文件名 3,谨慎的检查,友好的错误提示,精确定位文件上 ......

截止目前已经改造了3个类:

ubuntu:通过封装验证码类库一步步安装php的gd扩展

自定义MVC框架之工具类-分页类的封装

该文件上传类功能如下:

1,允许定制上传的文件类型,文件mime信息,文件大小

2,*定制文件名是随机还是保持原来的文件名

3,谨慎的检查,友好的错误提示,精确定位文件上传哪一步出问题

测试结果:

ghostwu@ghostwu:~/php/senior_php/upload$ tree
.
├── 19502QV8-6.jpg
├── Upload
│   ├── ghostwu_5a94beb3b2690.jpg
│   ├── ghostwu_5a94c727a1f1a.php
│   └── ghostwu_5a94c740615dc.html
├── upload.html
└── upload.php

upload.php:

自定义MVC框架之工具类-文件上传类
  1 <meta charset="utf-8" />
  2 <?php
  3     
  4 class Upload {
  5     //上传路径
  6     private $path = './Upload';
  7     //允许的文件类型
  8     private $allowFileType = [ 'jpg', 'png', 'jpeg', 'wbmp', 'gif' ];
  9     //允许的mime信息
 10     private $allowMime = [ 'image/jpeg', 'image/gif', 'image/png', 'image/wbmp' ];
 11     //允许的最大文件大小
 12     private $maxSize = 2048000;
 13     //运行随机文件名
 14     private $enableRandName = true;
 15     //文件前缀
 16     private $prefix = 'ghostwu_';
 17 
 18     //错误号
 19     private $errorNo;
 20     //错误信息
 21     private $errorInfo;
 22     
 23     //文件原来的名称
 24     private $originName;
 25     //新的文件名称
 26     private $newName;
 27     //文件后缀
 28     private $suffix;
 29     //文件大小
 30     private $size;
 31     //文件mime信息
 32     private $mime;
 33     //临时文件名
 34     private $tmpName;
 35 
 36 
 37     public function __construct( $fileInfo = [] ){
 38         foreach ($fileInfo as $k => $v ) {
 39             $this->setProperty( $k, $v );
 40         }
 41     }
 42 
 43     //设置成员属性
 44     public function setProperty( $k, $v ){
 45         $property = array_keys( get_class_vars( __CLASS__ ) );
 46         if( in_array( $k, $property ) ){
 47             $this->$k = $v;
 48         }
 49     }    
 50 
 51     public function __get( $key ) {
 52         if( $key == 'errorNo' ) {
 53             return $this->errorNo;
 54         }else if ( $key == 'errorInfo' ) {
 55             return $this->getErrorInfo();
 56         }
 57     }
 58 
 59     protected function getErrorInfo(){
 60         $info = '';
 61         switch( $this->errorNo ){
 62             case 1000:
 63                 $info = '没有设置上传路径';
 64                 break;
 65             case 1001:
 66                 $info = '上传路径不存在,或者没有写权限';
 67                 break;
 68             case 1002:
 69                 $info = '文件大小超过限制';    
 70                 break;
 71             case 1003:
 72                 $info = '文件的mime信息不在允许范围内';
 73                 break;
 74             case 1004:
 75                 $info = '不允许上传这种类型的文件';
 76                 break;
 77             case 1006:
 78                 $info = '文件不是通过表单上传的';
 79                 break;
 80             case 1007:
 81                 $info = '文件移动失败';
 82                 break;
 83             case 1:
 84                 $info = '文件超出php.ini设置的大小';
 85                 break;
 86             case 2:
 87                 $info = '超出html表单设置的大小';
 88                 break;
 89             case 3:
 90                 $info = '文件只有部分被上传';
 91                 break;
 92             case 4:
 93                 $info = '没有文件被上传';
 94                 break;
 95             case 6:
 96                 $info = '找不到临时文件';
 97                 break;
 98             case 7:
 99                 $info = '文件写入失败';
100                 break;
101         }
102 
103         return $info;
104     }
105 
106     protected function check(){
107         if( !file_exists( $this->path ) || !is_dir( $this->path ) ){
108             return mkdir( $this->path, 0777, true );
109         }
110         if( !is_writable( $this->path ) ){
111             return chmod( $this->path, 0777 );
112         }
113         return true;
114     }
115 
116     public function upload( $name ){
117         //判断是否设置了上传路径
118         if( empty( $this->path ) ) {
119             $this->setProperty( 'errorNo', 1000 );
120             return false;
121         }
122         //判断路径是否存在,可写
123         if( !$this->check() ) {
124             $this->setProperty( 'errorNo', 1001 );
125             return false;
126         }
127         //判断上传文件是否错误,提取原文件信息
128         $errorno = $_FILES[$name]['error'];
129         if( $errorno ) {
130             $this->setProperty( 'errorNo', $errorno );
131             return false;
132         }else {
133             $this->getFileInfo( $name );
134         }
135         //判断文件是否符合上传要求(大小,后缀,mime)
136         if( !$this->checkSize() 
137             || !$this->checkMime()
138             || !$this->checkSuffix() ) {
139             return false;
140         }
141         //生成新的文件名
142         $this->newName = $this->createNewName();
143         //判断是否为上传文件
144         if( is_uploaded_file( $this->tmpName ) ) {
145             if( move_uploaded_file( $this->tmpName, $this->path . '/' . $this->newName ) ){
146                 return $this->path . '/' . $this->newName;
147             }else {
148                 $this->setProperty( 'errorNo', 1007 );
149                 return false;
150             }
151         }else {
152             $this->setProperty( 'errorNo', 1006 );
153             return false;
154         }
155     }
156 
157     protected function createNewName(){
158         if ( $this->enableRandName ) {
159             $name = $this->prefix . uniqid() . '.' . $this->suffix;
160         }else {
161             $name = $this->prefix . $this->originName;
162         }
163         return $name;
164     }
165 
166     protected function getFileInfo( $name ){
167         $this->originName = $_FILES[$name]['name'];
168         $this->mime = $_FILES[$name]['type'];
169         print_r( $this->mime );
170         $this->tmpName = $_FILES[$name]['tmp_name'];    
171         $this->size = $_FILES[$name]['size'];
172         $this->suffix = pathinfo( $this->originName )['extension'];
173     }
174 
175     protected function checkSize(){
176         if( $this->size > $this->maxSize ) {
177             $this->setProperty( 'errorNo', 1002 );
178             return false;
179         }
180         return true;
181     }
182 
183     protected function checkMime(){
184             print_r( $this->allowMime );
185         if( !in_array( $this->mime, $this->allowMime ) ) {
186             $this->setProperty( 'errorNo', 1003 );
187             return false;
188         }
189         return true;
190     }
191 
192     protected function checkSuffix(){
193         if( !in_array( $this->suffix, $this->allowFileType ) ) {
194             $this->setProperty( 'errorNo', 1004 );
195             return false;
196         }
197         return true;
198     }
199 
200 }
201 
202 $upload = new Upload( [ 'maxSize' => 204800 ] );
203 $upload->setProperty( 'allowFileType', ['jpg','jpeg', 'gif', 'php', 'html'] );
204 $upload->setProperty( 'allowMime', [ 'image/jpeg', 'image/gif', 'image/wbmp', 'application/x-php', 'text/html'  ] );
205 
206 $upload->upload( 'photo' );
207 
208 echo $upload->errorNo . '<br/>';
209 echo $upload->errorInfo . '<br/>';
210 
211 ?>
View Code

upload.html

自定义MVC框架之工具类-文件上传类
 1 <!DOCTYPE html>
 2 <html>
 3         <head>
 4                 <meta charset="utf-8" />
 5                 <meta name="viewport" content="width=device-width" />
 6                 <title>文件上传-by ghostwu</title>
 7         </head>
 8         <body>
 9             <form action="upload.php" method="post" enctype="multipart/form-data" >
10                 <input type="file" name="photo" id="photo" />    
11                 <input type="submit" name="upload" id="upload" value="上传" />
12             </form>
13         </body>
14 </html>
View Code