常用类----上传类

把自己平常经常用的一系列的类写出来,温故而知新,期待在看过之后能对你有所帮助,并希望自己能再接再厉,能拿出更多的东西与大家共同分享:
php 代码
  1. <?php    
  2. /**     
  3.  *@package  Upload     
  4.  *@author   刘建武  MSN:tianhao86@hotmail.com     
  5.  *@uses     文件上传     
  6.  */      
  7.       
  8. class Upload{       
  9.       
  10.     /**     
  11.      *允许上传的文件大小     
  12.      */      
  13.     public $mSize = 2097152//2M       
  14.       
  15.     /**     
  16.      * 允许的上传文件的后缀     
  17.      */      
  18.     public $mExt = "jpg|png|gif|bmp";       
  19.       
  20.     /**     
  21.      * 上传路径     
  22.      */      
  23.     public $mPath ="./" ;       
  24.       
  25.     /**     
  26.      * 错误信息     
  27.      */      
  28.     public $mError;       
  29.            
  30.     /**     
  31.      * 设置上传路径      
  32.      */      
  33.     public function SetPath($pPath){       
  34.         return $this->mPath = $pPath;       
  35.     }       
  36.       
  37.     /**     
  38.      * 设置上传大小限制     
  39.      */      
  40.     public function SetSize($pSize){       
  41.         return  $this->mSize = $pSize;       
  42.     }       
  43.       
  44.     /**     
  45.      * 设置上传格式     
  46.      */      
  47.     public  function SetExt($pExt){       
  48.         return $this->mExt = $pExt;       
  49.     }       
  50.            
  51.     /**     
  52.      * 设置错误信息     
  53.      */      
  54.     public function SetErr($pID){       
  55.         return $this->mError = $pID;       
  56.     }       
  57.            
  58.     /**     
  59.      * 检测文件类型     
  60.      */      
  61.     public function CheckExt($pExt){       
  62.         $ext = explode("|", $this->mExt);       
  63.         if(in_array(strtolower($pExt), $ext)){       
  64.             return true;       
  65.         }       
  66.         else {       
  67.             return false;       
  68.         }       
  69.     }       
  70.       
  71.     /**     
  72.      * 检测文件大小     
  73.      */      
  74.     public function CheckSize($pSize){       
  75.         if($this->mSize > $pSize){       
  76.             return true;       
  77.         }       
  78.         else{       
  79.             return false;       
  80.         }       
  81.     }       
  82.       
  83.     /**     
  84.      * 上传文件     
  85.      */      
  86.       
  87.     public function Save($pForm){       
  88.                
  89.         $Form = $pForm;       
  90.         $name  = $_FILES[$Form]['name'];       
  91.         $tmp   = $_FILES[$Form]['tmp_name'];       
  92.         $size  = $_FILES[$Form]['size'];       
  93.         $temp  = explode(".", $name);       
  94.                
  95.         if(!file_exists($this->mPath)){       
  96.             $this->mError = 1;       
  97.             return false;       
  98.         }       
  99.                
  100.         if(!$this->CheckExt($temp[1])){       
  101.             $this->mError = 2;       
  102.             return false;          
  103.             //格式不正确       
  104.         }       
  105.                
  106.         if(!$this->CheckSize($size)){       
  107.             $this->mError = 3;       
  108.             return false;       
  109.             //大小不正确       
  110.         }       
  111.                
  112.         $out = time().mt_rand(0100);       
  113.         $file = $out.".".$temp[1];       
  114.         if(!move_uploaded_file($tmp, $this->mPath."/".$out.".".$temp[1])){       
  115.             return 4;       
  116.         }       
  117.         else {       
  118.             return $file;       
  119.         }       
  120.     }       
  121. }       
  122. ?>      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值