本文實例為大家分享了php文件上傳類的具體代碼,供大家參考,具體內容如下
<?php$upload = new UpLoad();$upload->uploadFile('fm');/*打印錯誤信息*/// var_dump($upload->errorNumber);// var_dump($upload->errorInfo);class UpLoad{  //文件上傳路徑  protected $path = 'upload/';  //允許文件上傳的后綴  protected $allowSuffix = ['jpg','jpeg',  'gif','wbmp','png'];  //mime類型   protected $allowMime =['image/jpg','image/jpeg',  'image/gif','image/wbmp','image/png'];  //允許上傳的大小  protected $maxSize = 2000000;  //是否啟用默認的前綴  protected $isRandName =true ;  //文件的前綴  protected $prefix = 'up_';  //錯誤號和錯誤信息  protected $errorNumber;  protected $errorInfo;  //文件的信息  //文件名  protected $oldName;  //文件的后綴  protected $suffix;  //文件的大小  protected $size;  //文件的mime  protected $mime;  //文件的臨時文件的路徑  protected $tmpName;  //文件新名字  protected $newName;    //構造方法  //因為成員屬性比較多就用數組來顯示  public function __construct($arr =[]){    foreach ($arr as $key=>$value){      $this->setOption($key,$value);    }  }  //判斷$key是不是我的成員屬性,如果是就設置  protected function setOption($key,$value){    //得到所有的成員屬性    $keys = array_keys(get_class_vars(__CLASS__));    if(in_array($key, $keys)){      $this->$key = $value;    }  }  //文件上傳函數  //key 就是input框中的name屬性值  public function uploadFile($key){    //判斷有沒有設置路徑 path    if(empty($this->path)){      $this->setOption('errorNumber',-1 );      return false;    }    //判斷該路徑是否存在是否可寫    if (!$this->check()){      $this->setOption('errorNumber', -2);      return false;     }    //判斷$_FILES里面的error信息是否為0,如果為0則說明文件信息在服務器端可以直接獲取,提取信息保存到成員屬性中    $error = $_FILES[$key]['error'];    if($error){      $this->setOption('errorNumber', -3);      return false;    }else {      //提取文件相關信息并且保存到成員屬性中      $this->getFileInfo($key);    }    //判斷文件的大小、mime、后綴是否符合     if(!$this->checkSize() || !$this->checkMime()|| !$this->checkSuffix()){       return false;     }    //得到新的文件名字    $this->newName = $this->createNewName();    //判斷是否是上傳文件,并且是移動上傳文件    if(is_uploaded_file($this->tmpName)){      if(move_uploaded_file($this->tmpName, $this->path.$this->newName)){        return $this->path.$this->newName;      }else {        $this->setOption('errorNumber', -7);        return false;      }    }else{      $this->setOption('errorNumber', -6);      return false;    }  }  //檢測文件夾是否存在,是否可寫  protected function check(){    //文件夾不存在或者不是目錄。創建文件夾    if(!file_exists($this->path) ||!is_dir($this->path)){      return mkdir($this->path,0777,true);    }    //判斷文件是否可寫    if(!is_writeable($this->path)){      return chmod($this->path, 0777);    }    return true;  }  //根據key得到文件信息  protected function getFileInfo($key){    //得到文件的名字    $this->oldName = $_FILES[$key]['name'];    //得到文件的mime類型    $this->mime = $_FILES[$key]['type'];    //得到文件的臨時文件    $this->tmpName = $_FILES[$key]['tmp_name'];    //得到文件大小    $this->size = $_FILES[$key]['size'];    //得到文件后綴    $this->suffix = pathinfo($this->oldName)['extension'];  }  //判斷文件大小  protected function checkSize(){    if($this->size > $this->maxSize){      $this->setOption('errorNumber', -3);      return false;    }    return true;  }  //判斷mime類型  protected function checkMime(){    if(!in_array($this->mime, $this->allowMime)){      $this->setOption('errorNumber', -4);      return false;    }    return true;  }  //判斷后綴  protected function checkSuffix(){    if(!in_array($this->suffix, $this->allowSuffix)){      $this->setOption('errorNumber', -5);      return false;    }    return true;  }  //創建新名字  protected function createNewName(){    if($this->isRandName){      $name = $this->prefix.uniqid().'.'.$this->suffix;    }else {      $name = $this->prefix.$this->oldName;    }    return $name;  }  public function __get($name){    if($name == 'errorNumber'){      return $this->errorNumber;    }elseif ($name == 'errorInfo'){      return $this->getErrorInfo();    }  }  protected function getErrorInfo(){    switch ($this->errorNumber){    case -1:      $str = '文件路徑沒有設置';      break;    case -2:      $str = '文件不是目錄或者不可寫';      break;    case -3:      $str = '文件超過指定大小';      break;    case -4:      $str = 'mime類型不符合';      break;    case -5:      $str = '文件后綴不符合';      break;    case -6:      $str = '不是上傳文件';      break;    case -7:      $str = '移動失敗';      break;    case 1:      $str = '超出ini設置大小';      break;    case 2:      $str = '超出html表單大小';      break;    case 3:      $str = '文章只有部分上傳';      break;    case 4:      $str = '沒有文件上傳';      break;    case 6:      $str = '找不到臨時文件';      break;    case 7:      $str = '文件寫入失敗';      break;    }    return $str;  }}<!doctype html><html lang="en"><head> <meta charset="UTF-8" /> <title>文件上傳</title></head><body><form action="UpLoad.php" method="post" enctype="multipart/form-data" > <input type="file" name="fm" value=""><br> <input type="submit" value="上傳文件" /><br> </form></body></html>
注意:input中的name必須和上傳類中的uploadFile中是傳值一致!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答
圖片精選