Blogger Information
Blog 39
fans 0
comment 0
visits 34069
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
文件上传类与自定义的异常类抛出--2019/08/08
LISTEN的博客
Original
760 people have browsed it

将课堂中的文件上传案例中的错误信息, 全部采用自定义的异常类抛出


1、文件上传html页面 index.html

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上传文件</title>
</head>
<body>
<form action="my_work.php" method="post" enctype="multipart/form-data">
    <!--设置上传文件大小最大为3M  这段代码一定要放到文件提交框之前-->
    <!--    1M = 1024K = 1024*1024Byte=1048576Byte-->
    <input type="hidden" name="MAX_FILE_SIZE" value="3145728">
    <input type="file" name="my_file" id="">

    <!--button默认为提交按钮-->
    <button>上传</button>

</form>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


2、自定义异常类  CustomException.php

实例

<?php

// 自定义异常类
class CustomException extends Exception
{

    public function errorInfo()
    {
        $str="<h3>
                  <strong>错误代码:{$this->getCode()} --- </strong>
                  <span style='color: red'>{$this->getMessage()}</span>
              </h3>";
        return $str;

    }

}

运行实例 »

点击 "运行实例" 按钮查看在线实例


3、文件上传类  FileUpload.php

实例

<?php
require 'CustomException.php';

class FileUpload
{
    public $flie;
    // 原始文件名称
    protected $fileName;
    // 临时文件名
    protected $fileTmpName;
    //文件类型
    protected $fileType;
    //文件错误代码
    protected $fileError;
    //文件大小
    protected $fileSize;
    //文件保存路径
    protected $savePath;
    //保存的文件名
    protected $saveFileName;
    // 允许上传的文件后缀
    protected $allowExts=['jpg','jpeg','png','gif'];


    public function __construct($file,$savePath='')
    {
        $this->file=$file;
        $this->fileName=$file['name'];
        $this->fileTmpName=$file['tmp_name'];
        $this->fileType=$file['type'];
        $this->fileError=$file['error'];
        $this->fileSize=$file['size'];
        $this->savePath=$savePath;
        if (!file_exists($savePath)){
            mkdir($savePath,0777,true);//创建多级目录
        }
    }

    public function __get($name)
    {
        return $this->$name;
    }

    public function __set($name, $value)
    {
        $this->$name=$value;
    }

    //文件上传
    public function upload()
    {
        // 判断是否上传成功
        $this->getFileError();

        //判断文件后缀名是否符合要求
        $extension=$this->getExtension();

        $this->saveFileName=date('YmdHis',time()).mt_rand(1,99).'.'.$extension;

        if(is_uploaded_file($this->fileTmpName)){
            if (move_uploaded_file($this->fileTmpName,$this->savePath.$this->saveFileName)){
                echo '<script>alert("上传成功");history.back();</script>';
            }else{
                throw new CustomException('文件无法移动到指定目录, 请检查目录的写权限',103);
            }
        }else{
            throw new CustomException('非法操作',102);
        }

    }

    //判断是否上传成功
    protected function getFileError()
    {
        if($this->fileError>0){
            switch ($this->fileError){
                case 1:
                    throw new CustomException('上传的文件超过了 php.ini 中 upload_max_filesize选项限制的值。',1);
                case 2:
                    throw new CustomException('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。上传文件不允许超过3M',2);
                case 3:
                    throw new CustomException('文件只有部分被上传。上传文件不完整',3);
                case 4:
                    throw new CustomException('没有文件被上传',4);
                case 6:
                    throw new CustomException('找不到临时文件夹',6);
                case 7:
                    throw new CustomException('文件写入失败',7);
                default:
                    throw new CustomException('未知错误',8);
            }
        }
    }

    //判断文件后缀名是否符合要求
    protected function getExtension()
    {
        $myfile=explode('.',$this->fileName);
        $extension=array_pop($myfile);
        if(!in_array($extension,$this->allowExts)){
            throw new CustomException('不允许上传'.$extension.'文件类型',101);
        }
        return $extension;
    }

}

运行实例 »

点击 "运行实例" 按钮查看在线实例


4、文件上传调用 my_work.php

实例

<?php
require 'FileUpload.php';

try{
    $obj=new FileUpload($_FILES['my_file'],'uploads/img/');
    $obj->upload();
}catch (CustomException $e){
    echo $e->errorInfo();
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

 

Correction status:qualified

Teacher's comments:写得不错.思路很清楚
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post