[练手]小小上传类

  1 class Upload {
  2   protected  $allowext =  array('jpg','bmp','gif','png','jpeg');//允许的上传
  3   protected  $file = array();//记录上传文件的信息
  4   protected  $maxsize = 1;//设置最大的上传大小
  5   protected  $erron = 0;//获取当前的错误错误号码
  6 
  7   //erron 数组图
  8   protected  $error = array(
  9                     0 => '上传成功',
 10                     1 => '文件超出PHP.ini的uploade_max_filesize',
 11                     2 => '文件超出了HTML表单中设置的MAX_FILE_SIZE的最大值',
 12                     3 => '文件只有部分被上传',
 13                     4 => '没有文件被上传',
 14                     5 => '找不到临时的文件夹',
 15                     6 => '文件写入失败',
 16                     7 => '不支持的文件格式上传',
 17                     8 => '超过网站大小限制',
 18                     9 => '未知错....'
 19                     );
 20 
 21     /*获取上传文件的后缀*/
 22     public function getExt(){
 23              if(!$ext =strtolower(strrchr($this->file['oldname'], '.'))){
 24                 return false;
 25              }else{
 26                 return $ext;
 27              }
 28     }
 29 
 30     /*产生四个随机字符*/
 31     public function randStr($num=4){
 32         $str = 'QWERTYUIPASDFGHJKMNBVCXZqwertyuipasdfghjkmnbvcxz123456789';
 33         return $str = substr(str_shuffle($str), 0,$num);
 34     }
 35 
 36     /*递归的创建目录*/
 37     public function createDir(){
 38         $path = 'upload/'.date('Y/md');
 39         if(is_dir($path)){
 40           return $path;
 41         }else{
 42           mkdir($path,0777,true);
 43         }
 44         return $path;
 45     }
 46 
 47     /*过滤文件类型*/
 48     public function fileType(){
 49         if(!in_array(substr($this->file['type'],6), $this->allowext)){
 50           $this->erron =7; 
 51           return false;
 52         }
 53         return true;
 54     }
 55     /*过滤大小*/
 56     public function fileSize(){
 57         if($this->file['size']>=$this->maxsize*1000*1000){
 58             $this->erron = 8;
 59             return false;
 60         }
 61         return true;
 62 
 63     }
 64 
 65     /*开始上传*/
 66     public function fUpload($name){
 67       $arr = array();
 68       $this->erron = $_FILES[$name]['error'];
 69       if($this->erron>0){
 70         echo $this->error[$this->erron];
 71         return false;
 72       }
 73       $this->file['oldname'] = $_FILES[$name]['name'];
 74       $this->file['tmp_name'] = $_FILES[$name]['tmp_name'];
 75       $this->file ['type'] = $_FILES[$name]['type'];
 76       $this->file['size'] = $_FILES[$name]['size'];
 77       $this->file['ext'] = $this->getExt();
 78       $this->file['name'] = $this->randStr().$this->getExt();
 79 
 80 
 81       if(!$this->fileType()){
 82         echo $this->error[$this->erron];
 83         return false;
 84       }
 85 
 86       if(!$this->fileSize()){
 87         echo $this->error[$this->erron];
 88         return false;
 89       }
 90       
 91 
 92       $path = $this->createDir().'/'.$this->file['name'];
 93 
 94 
 95       move_uploaded_file($this->file['tmp_name'], $path);
 96       echo $this->error[$this->erron];
 97       $arr['path'] = $path;
 98       $arr['oldname'] = $this->file['oldname'];
 99       $arr['size'] = $this->file['size']; 
100       $arr['name'] = $this ->file['name'];
101 
102       return $arr;
103 
104     }
105 
106 
107 
108 }

 

posted @ 2014-10-08 16:26  不能没有Ni  阅读(120)  评论(0)    收藏  举报