Blogger Information
Blog 4
fans 0
comment 0
visits 2168
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
异常/模型/文件上传-2019年8月9日15:14
gaoqi的博客
Original
447 people have browsed it

遇到的问题如下,请老师指导下

1、将一个错误信息抛出时,可以正常显示

2、当添加第2个错误信息时,显示异常,不知道这种方式问题出在哪


QQ图片20190809151808.png

实例

<?php

$fileType = ['jpg', 'jpeg', 'png', 'gif'];
$fileSize = 28;
// 文件上传到服务器上的目录
$filePath = '/uploads/';
// 原始文件名称
$fileName = $_FILES['my_file']['name'];
// 临时文件名
$tmpFile = $_FILES['my_file']['tmp_name'];

// 2. 判断是否上传成功?
$uploadError = $_FILES['my_file']['error'];

$extension = explode('.', $fileName)[1];



class customException extends Exception
{
    public function errorMessage()
    {
        // 错误信息
        $errorMsg ='<b>'.$this->getMessage().'</b> 是不允许上传的文件类型';
        return $errorMsg;
    }
    public function errorMessage2()
    {
        // 错误信息
        $errorMsg ='<b>'.$this->getMessage().'</b> 上传文件不允许超过3M';
        return $errorMsg;
    }




}
 
 
try
{

if (!in_array($extension, $fileType)) {
    
    throw new customException($extension);
}

if ( $uploadError == 2 ) {

    
    throw new customException($uploadError);

}


  

}
catch (customException $e)
{
    echo $e->errorMessage();
}
catch (customException $e)
{
    echo $e->errorMessage2();
}




if (is_uploaded_file($tmpFile)) {
    if (move_uploaded_file($tmpFile, __DIR__ . $filePath. $fileName)) {
        echo '<script>alert("上传成功");history.back();</script>';
    } else {
        die('文件无法移动到指定目录, 请检查目录的写权限');
    }
} else {
    die('非法操作');
}


exit();

?>

运行实例 »

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

Correction status:qualified

Teacher's comments:抛出异常后,应该终止代码, 添加上die/exit
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