php文件上传类

FileUpload.class.php:

<?php
   class FileUpload{
    //设置成员属性
    private $filepath='./upload/';//设置文件上传路径
    private $allowtype=array('jpg','pgn','gif');//设置允许上传的文件类型
    private $maxsize=1024000;//设置允许上传的文件最大值
    private $israndname=false;//设置文件名是否随机
    private $name;//设置文件名称
    private $tmp_name;//设置临时文件名称
    private $error;//设置错误数值
    private $size;//设置文件尺寸
    private $type;//设置文件类型
    private $errorNum;//设置错误号
    public  $fileMsg;//设置文件信息
       //参数初始化
    function __construct($options = array()){
     foreach($options as $key=>$val){
         if(array_key_exists($key,get_class_vars(get_class($this)))){
                     $this->setOption($key,$val);
     }
     }
    }
    private function setOption($key,$val){
           $this->$key=$val;
    }
    private function getError(){
     $str="上传文件<font color='red'>{$this->name}</font>时出错:";
     switch($this->errorNum){
       case 4: $str .= "没有文件被上传"; break;
    case 3: $str .= "文件只被部分上传"; break;
    case 2: $str .= "上传文件超过了HTML表单中MAX_FILE_SIZE选项指定的值"; break;
    case 1: $str .= "上传文件超过了php.ini 中upload_max_filesize选项的值"; break;
    case -1: $str .= "未允许的类型"; break;
    case -2: $str .= "文件过大,上传文件不能超过{$this->maxSize}个字节"; break;
    case -3: $str .= "上传失败"; break;
    case -4: $str .= "建立存放上传文件目录失败,请重新指定上传目录"; break;
    case -5: $str .= "必须指定上传文件的路径"; break;
    default: $str .=  "未知错误";
     }
           return $str;
    }
    //随机文件名
    private function setNewFileName(){
     if($this->israndname){
         $this->newFileName=date(YmdHis).rand(100,999).'.'.$this->type;
     }else{
      $this->newFileName=$this->name;
     }
    }
    //检查文件错误
    private function checkError(){
     if($this->error){
      $this->setOption('errorNum',$this->error);
      return false;
     }else{
      return true;
     }
    }
    //检查文件类型
    private function checkType(){
     if(in_array($this->type,$this->allowtype)){
           return true;
     }else{
        $this->setOption('errorNum',-1);
     return false;
        }
    }
    //检查文件大小
    private function checkSize(){
         if($this->size < $this->maxsize){
                return true;
   }else{
    $this->setOption('errorNum',-2);
    return false;
   }
    }
    //检查文件上传路径
    private function checkFilePath(){
     if(empty($this->filepath)){
          $this->setOption('errorNum',-5);
    return false;
     }else{
       if(!file_exists($this->filepath) || !is_writable($this->filepath)){
     if(!mkdir($this->filepath,0755)){
                        $this->setOption('errorNum',-4);
      return false;
     }
    }
       return true;
     }
    }
    //拷贝文件                                此处临时 1get需要在copy前执行
    private function copyFile(){
     $destination=rtrim($this->filepath,'/').'/';
     $destination.=$this->newFileName;
     if(move_uploaded_file($this->tmp_name,$destination)){
      return true;
     }else{
      $this->setOption('errorNum',-3);
      return false;
     }
    }
    //获取文件参数,并上传文件
    function fileUpload($fileField){
     if(!$this->checkFilePath()){
         $this->fileMsg[]=$this->getError();
      return fileMsg;
     }
  $name=$_FILES[$fileField]['name'];    
  for($i=0;$i<count($name);$i++){
           $this->name=$_FILES[$fileField]['name'][$i];
     $this->tmp_name=$_FILES[$fileField]['tmp_name'][$i];
     $this->size=$_FILES[$fileField]['size'][$i];
     $this->error=$_FILES[$fileField]['error'][$i];
     $arrType=explode('.',$_FILES[$fileField]['name'][$i]);
     $this->type=$arrType[count($arrType)-1];
           if($this->checkError() && $this->checkType() && $this->checkSize()){
      $this->setNewFileName();
      if($this->copyFile()){
       $this->fileMsg[]=$this->newFileName;
      }else{
       $this->fileMsg[]=$this->getError();
      }
     }else{
       $this->fileMsg[]=$this->getError();
     }
    }
    return $this->fileMsg;
    } 
}
?>   

upload.php:

<?php
   require "FileUpload.class.php";
   $up=new FileUpload(array('allowtype'=>array('php','txt','jpg','png'),'hi'=>'hello','filepath'=>'./upload/','israndname'=>true));  
   echo '<pre>';
   $up->fileUpload('spic');
   print_r($up->fileMsg);
   echo '</pre>';
   //var_dump($up);
?>

form.html:

<html>
  <head>
     <title>关于网页上传</title>
  </head>
  <body>
    <form action="upload.php" method="post" enctype="multipart/form-data">
     <input type="hidden" name="MAX_FILE_SIZE" value="100000000">
     <input type="file" name="spic[]"> <br>
  <input type="file" name="spic[]"> <br>
  <input type="file" name="spic[]"> <br>
     <input type="submit" name="sub" value="upload file"><br>
    </form>
  </body>
</html>

这是我写的文件上传类,分享一下,希望大家提出宝贵意见

 

posted @ 2013-03-07 00:53  达也.  阅读(212)  评论(0)    收藏  举报