• PHP上传文件类 代码练习


    类文件:

    <?php
    
    class upload{
        
        protected $fileName;
        protected $uploadPath;
        protected $maxSize;
        protected $extarray;
        protected $checkimg;
        protected $mimeType;
        protected $fileInfo;
        protected $error;
        protected $ext;
    
          public function __construct($fileName='filename',$uploadPath='uploads',$maxSize=2097152,$checkimg=true,$extarray=['jpeg','jpg','png','gif'],$mimeType=['image/jpeg','image/png','image/gif']){
              $this->fileName=$fileName;
              $this->uploadPath=$uploadPath;
              $this->maxSize=$maxSize;
              $this->checkimg=$checkimg;
              $this->extarray=$extarray;
              $this->mimeType=$mimeType;
              $this->fileInfo=$_FILES[$this->fileName];
    
          }
    
    
         //判断错误号
          protected function checkError(){
              if(!is_null($this->fileInfo)){
                  if($this->fileInfo['error']>0){
                      switch ($this->fileInfo['error']) {
                          case 1:
                              $this->error='文件超过了PHP配置中文件的大小';
                              break;
                          
                          case 2:
                              $this->error='文件超过了表单中的限制';
                              break;
                          case 3:
                              $this->error='文件部分被上传';
                              break;
                          case 4:
                              $this->error='没有选择上传文件';
                              break;
                          case 6:
                              $this->error='没有找到临时目录';
                              break;
                          case 7:
                              $this->error='文件不可写';
                              break;
                          case 8:
                              $this->error='由于PHP的扩展程序中断文件上传';
                              break;
    
                      }
                      return false;
                  }
                  return true;
              }
              $this->error='文件上传出错';
              return false;
    
          }
    
          //判断文件大小
          protected function checkSize(){
              if($this->fileInfo['size']>$this->maxSize){
                  $this->error='文件过大';
                  return false;
              }
              return true;
    
          }
    
          //判断文件类型
          protected function getExt(){
              $this->ext=strtolower(pathinfo($this->fileInfo['name'],PATHINFO_EXTENSION));
              return $this->ext;
          }
          protected function checkExt(){
              $this->getExt();
              if(!in_array($this->ext, $this->extarray)){
                  $this->error='文件格式不正确,允许的格式为:jpeg,jpg,png,gif';
                  return false;
    
              }
              return true;
          }
    
          //判断mime类型
          protected function checkMime(){
              if(!in_array($this->fileInfo['type'],$this->mimeType)){
                  $this->error='不允许的文件类型';
                  return false;
              }
              return true;
          }
    
          //判断是否是真正的图片
    
          protected function checkImage(){
              if ($this->checkimg) {
                  if(@!getimagesize($this->fileInfo['tmp_name'])){
                      $this->error='不是真正的图片';
                      return false;
                  }
                  return true;
                  # code...
              }
          }
    
    
    
         //判断是否是通过HTTP post方式上传
          protected function checkHttppost(){
              if(!is_uploaded_file($this->fileInfo['tmp_name'])){
                  $this->error='不是通过HTTP post方式上传';
                  return false;
              }
              return true;
          }
    
          //显示错误
          protected function showError(){
              exit('<span style="color:red">'.$this->error.'</span>');
          }
    
    
          //检查上传目录并创建
          protected function checkPath(){
              if(!file_exists($this->uploadPath)){
                  mkdir($this->uploadPath,0777,true);
              }
          }
    
          //创建唯一的文件名
          protected function getName(){
              return md5(uniqid(microtime(true),true)).'.'.$this->ext;
    
          }
    
          //上传文件
          public function uploadFile(){
              if ($this->checkError()&&$this->checkSize()&&$this->checkExt()&&$this->checkMime()/*&&$this->checkImage()*/&&$this->checkHttppost()) {
    
                  $this->checkPath();
                  $this->uniName=$this->getName();
                  $this->destination=$this->uploadPath.'/'.$this->uniName;
                      
                  if(@move_uploaded_file($this->fileInfo['tmp_name'], $this->destination)){
    
                      return $this->destination;
                  }else{
                      $this->error='文件移动失败';
                      $this->showError();
                  }
                  # code...
              }else{
                  $this->showError();
              }
          }
    }

    表单:

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
        <title>无标题</title>
    </head>
    <body>
      <form action="doaction.php" method="post" enctype="multipart/form-data">
      <input type="hidden" name="MAX_FILE_SIZE" value="2097152">
      <input type="file" name="filename" accept="image/jpeg"><br/><br/>
      <input type="submit" value="上传">
      </form>
    </body>
    </html>

    执行文件:

    <?php
    require('uploads.class.php');
    
    $upload=new upload('filename','uploads',$maxSize=2097152,$checkimg=false);
    
    $dest=$upload->uploadFile();
    
    echo $dest;
  • 相关阅读:
    【微服务架构】SpringCloud组件和概念介绍(一)
    getaddrinfo()详解
    通货膨胀背景下,只能好好搞技术了
    到此为止,重新开始
    二级指针
    一篇关于spice vdagent和利用virtmanager配置spice的文章
    局域网中两台无线路由器联接配置方法图文教程
    ovirt简介
    IIS fastcgi 超时处理
    关于eclipse无法自动编译的问题
  • 原文地址:https://www.cnblogs.com/perseverancevictory/p/4286611.html
Copyright © 2020-2023  润新知