欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php文件上傳類(lèi)的分享

 更新時(shí)間:2017年07月06日 09:52:17   作者:東東東蔚  
這篇文章主要為大家分享了php文件上傳類(lèi)的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了php文件上傳類(lèi)的具體代碼,供大家參考,具體內(nèi)容如下

<?php
$upload = new UpLoad();
$upload->uploadFile('fm');
/*打印錯(cuò)誤信息*/
// var_dump($upload->errorNumber);
// var_dump($upload->errorInfo);
class UpLoad{
  //文件上傳路徑
  protected $path = 'upload/';
  //允許文件上傳的后綴
  protected $allowSuffix = ['jpg','jpeg',
  'gif','wbmp','png'];
  //mime類(lèi)型 
  protected $allowMime =['image/jpg','image/jpeg',
  'image/gif','image/wbmp','image/png'];
  //允許上傳的大小
  protected $maxSize = 2000000;
  //是否啟用默認(rèn)的前綴
  protected $isRandName =true ;
  //文件的前綴
  protected $prefix = 'up_';
  //錯(cuò)誤號(hào)和錯(cuò)誤信息
  protected $errorNumber;
  protected $errorInfo;
  //文件的信息
  //文件名
  protected $oldName;
  //文件的后綴
  protected $suffix;
  //文件的大小
  protected $size;
  //文件的mime
  protected $mime;
  //文件的臨時(shí)文件的路徑
  protected $tmpName;
  //文件新名字
  protected $newName;
  
  //構(gòu)造方法
  //因?yàn)槌蓡T屬性比較多就用數(shù)組來(lái)顯示
  public function __construct($arr =[]){
    foreach ($arr as $key=>$value){
      $this->setOption($key,$value);
    }
  }
  //判斷$key是不是我的成員屬性,如果是就設(shè)置
  protected function setOption($key,$value){
    //得到所有的成員屬性
    $keys = array_keys(get_class_vars(__CLASS__));
    if(in_array($key, $keys)){
      $this->$key = $value;
    }
  }
  //文件上傳函數(shù)
  //key 就是input框中的name屬性值
  public function uploadFile($key){
    //判斷有沒(méi)有設(shè)置路徑 path
    if(empty($this->path)){
      $this->setOption('errorNumber',-1 );
      return false;
    }
    //判斷該路徑是否存在是否可寫(xiě)
    if (!$this->check()){
      $this->setOption('errorNumber', -2);
      return false; 
    }
    //判斷$_FILES里面的error信息是否為0,如果為0則說(shuō)明文件信息在服務(wù)器端可以直接獲取,提取信息保存到成員屬性中
    $error = $_FILES[$key]['error'];
    if($error){
      $this->setOption('errorNumber', -3);
      return false;
    }else {
      //提取文件相關(guān)信息并且保存到成員屬性中
      $this->getFileInfo($key);
    }
    //判斷文件的大小、mime、后綴是否符合
     if(!$this->checkSize() || !$this->checkMime()|| !$this->checkSuffix()){
       return false;
     }
    //得到新的文件名字
    $this->newName = $this->createNewName();
    //判斷是否是上傳文件,并且是移動(dòng)上傳文件
    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;
    }
  }
  //檢測(cè)文件夾是否存在,是否可寫(xiě)
  protected function check(){
    //文件夾不存在或者不是目錄。創(chuàng)建文件夾
    if(!file_exists($this->path) ||!is_dir($this->path)){
      return mkdir($this->path,0777,true);
    }
    //判斷文件是否可寫(xiě)
    if(!is_writeable($this->path)){
      return chmod($this->path, 0777);
    }
    return true;
  }
  //根據(jù)key得到文件信息
  protected function getFileInfo($key){
    //得到文件的名字
    $this->oldName = $_FILES[$key]['name'];
    //得到文件的mime類(lèi)型
    $this->mime = $_FILES[$key]['type'];
    //得到文件的臨時(shí)文件
    $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類(lèi)型
  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;
  }
  //創(chuàng)建新名字
  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 = '文件路徑?jīng)]有設(shè)置';
      break;
    case -2:
      $str = '文件不是目錄或者不可寫(xiě)';
      break;
    case -3:
      $str = '文件超過(guò)指定大小';
      break;
    case -4:
      $str = 'mime類(lèi)型不符合';
      break;
    case -5:
      $str = '文件后綴不符合';
      break;
    case -6:
      $str = '不是上傳文件';
      break;
    case -7:
      $str = '移動(dòng)失敗';
      break;
    case 1:
      $str = '超出ini設(shè)置大小';
      break;
    case 2:
      $str = '超出html表單大小';
      break;
    case 3:
      $str = '文章只有部分上傳';
      break;
    case 4:
      $str = '沒(méi)有文件上傳';
      break;
    case 6:
      $str = '找不到臨時(shí)文件';
      break;
    case 7:
      $str = '文件寫(xiě)入失敗';
      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必須和上傳類(lèi)中的uploadFile中是傳值一致!

更多精彩內(nèi)容請(qǐng)參考php文件上傳操作匯總進(jìn)行學(xué)習(xí)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論