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;

 

你可能感兴趣的:(上传文件)