文件上传类(引用)

  1 <?php
  2 
  3 class TL_Update_File{
  4     private $file = null;//接受图片名称
  5     private $files = [];//图片属性
  6     public $size = null;//上传文件大小限制
  7     public $save_path = null;//保存路径
  8     public $mime = null;//文件上传支持类型
  9     private $error = null;//图片不符合要求者
 10     //接受图片
 11     function __construct($file=null){
 12         $this->save_path = str_replace("\\","/",dirname(__FILE__))."/";
 13         $this->file = $file;
 14         if($file){
 15             $this->files = $_FILES[$file];
 16         }else{
 17             $this->files = $_FILES;
 18         }
 19     }
 20     //一系列属性向
 21     private function attribute(){
 22         if($this->file){
 23             $this->empty_ture();
 24         }else{
 25             $this->empty_false();
 26         }
 27         if($this->size){
 28             if($this->file){
 29                 $this->Size_ture();
 30             }else{
 31                 $this->Size_false();
 32             }
 33         }
 34         if($this->mime){
 35             if($this->file){
 36                 $this->mime_ture();
 37             }else{
 38                 $this->mime_false();
 39             }
 40         }
 41     }
 42     //判断图片是否为空
 43     function empty_false(){
 44         foreach ($this->files as $key => $value){
 45             if($this->files[$key]['error']){
 46                 $this->error[$key] = $value;
 47                 $this->error[$key]['tl_error'] = "改图片为空";
 48                 unset($this->files[$key]);
 49             }else{
 50                 $this->files[$key]['mime'] = substr($value['name'], strrpos($value['name'], ".")+1);
 51             }
 52         }
 53     }
 54     //判断图片是否为空
 55     function empty_ture(){
 56         if($this->files['error']) {
 57             $this->error[$this->file] = $this->files;
 58             $this->error[$this->file]['tl_error'] = "改图片为空";
 59             $this->files=[];
 60         }else{
 61             $this->files['mime'] = substr($this->files['name'], strrpos($this->files['name'], ".") + 1);
 62         }
 63     }
 64     //文件后缀名是否受限制
 65     function mime_ture(){
 66         if($this->files){
 67             if(!in_array($this->files['mime'],$this->mime)){
 68                 $this->error[$this->file] = $this->files;
 69                 $this->error[$this->file]['tl_error'] = "不支持改后缀名";
 70                 $this->files=[];
 71             }
 72         }
 73     }
 74     //文件后缀名是否受限制
 75     function mime_false(){
 76         foreach ($this->files as $key => $value){
 77             if(!in_array($this->files[$key]['mime'],$this->mime)){
 78                 $this->error[$key] = $value;
 79                 $this->error[$key]['tl_error'] = "不支持改后缀名";
 80                 unset($this->files[$key]);
 81             }
 82         }
 83     }
 84     //文件大小是否受限制
 85     function Size_ture(){
 86         if($this->files['size'] > $this->size){
 87             $this->error[$this->file] = $this->files;
 88             $this->error[$this->file] = $this->files;
 89             $this->error[$this->file]['tl_error'] = "文件过大";
 90             $this->files=[];
 91         }
 92     }
 93     //文件大小是否受限制
 94     function Size_false(){
 95         foreach ($this->files as $key => $value){
 96             if($this->files[$key]['size'] > $this->size){
 97                 $this->error[$key] = $value;
 98                 $this->error[$key]['tl_error'] = "文件过大";
 99                 unset($this->files[$key]);
100             }
101         }
102     }
103     //保存图片
104     function save_file(){
105         if($this->files){
106             $file_path_dir = date('Ym',time()) ."/";
107             $file_name = time() . rand(00000,99999) . "." . $this->files["mime"];
108             $this->is_dir_on_off($this->save_path . date('Ym',time()) ."/");
109             if($this->file){
110                 $this->save_file_ture($file_path_dir,$file_name);
111             }else{
112                 $this->save_file_false($file_path_dir,$file_name);
113             }
114         }
115     }
116     //保存图片
117     function save_file_ture($file_path_dir,$file_name){
118         move_uploaded_file($this->files["tmp_name"] , $this->save_path . $file_path_dir . $file_name);
119     }
120     //保存图片
121     function save_file_false($file_path_dir,$file_name){
122         foreach ($this->files as $key => $value){
123             move_uploaded_file($this->files[$key]["tmp_name"] , $this->save_path . $file_path_dir . $file_name);
124         }
125     }
126     //创建目录
127     function is_dir_on_off($file_path){
128         if(!is_dir($file_path)){
129             mkdir($file_path,0777);
130         }
131     }
132     //保存文件
133     function move(){
134         $this->attribute();
135         $this->save_file();
136     }
137 }
138 文章来源:http://www.cnblogs.com/hello-tl/p/7593033.html

 

posted on 2018-05-11 14:21  xuxxnb  阅读(130)  评论(0编辑  收藏  举报

导航