国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php文件上傳類的分享

2024-05-04 23:58:34
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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武林網。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 浦县| 夏河县| 衡南县| 和平县| 竹山县| 石门县| 宝山区| 施秉县| 涿鹿县| 西昌市| 横山县| 尚义县| 固安县| 类乌齐县| 扎兰屯市| 宝清县| 砚山县| 婺源县| 中阳县| 泾阳县| 鄂托克前旗| 隆子县| 邢台县| 许昌县| 侯马市| 高淳县| 松江区| 扬州市| 阿荣旗| 白城市| 乌拉特前旗| 永泰县| 达拉特旗| 永州市| 南宫市| 合江县| 游戏| 巴彦淖尔市| 潼南县| 扶余县| 正蓝旗|