亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 32
粉絲 0
評論 0
訪問量 27812
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
08-08作業(yè):將課堂中的文件上傳案例中的錯(cuò)誤信息, 全部采用自定義的異常類拋出
Yx的博客
原創(chuàng)
828人瀏覽過

1.  文件上傳html頁面實(shí)例:

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

    <!--button默認(rèn)為提交按鈕-->
    <button>上傳</button>

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

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

2.  自定義異常類頁面CustomException.php實(shí)例:

<?php

// 自定義異常類
class CustomException extends Exception
{

    public function errorInfo()
    {
        $str="<h3>
                  <strong>錯(cuò)誤代碼:{$this->getCode()} --- </strong>
                  <span style='color: red'>{$this->getMessage()}</span>
              </h3>";
        return $str;

    }

}

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

3.   文件上傳類FUpload實(shí)例:

<?php
require 'CustomException.php';  //引入CustomException.php

class FUpload
{
    public $flie;
    // 原始文件名稱
    protected $fileName;
    // 臨時(shí)文件名
    protected $fileTmpName;
    //文件類型
    protected $fileType;
    //文件錯(cuò)誤代碼
    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,2019,true);//創(chuàng)建多級目錄
        }
    }

    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('文件無法移動(dòng)到指定目錄, 請檢查目錄的寫權(quán)限',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選項(xiàng)限制的值。',1);
                case 2:
                    throw new CustomException('上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值。上傳文件不允許超過3M',2);
                case 3:
                    throw new CustomException('文件只有部分被上傳。上傳文件不完整',3);
                case 4:
                    throw new CustomException('沒有文件被上傳',4);
                case 6:
                    throw new CustomException('找不到臨時(shí)文件夾',6);
                case 7:
                    throw new CustomException('文件寫入失敗',7);
                default:
                    throw new CustomException('未知錯(cuò)誤',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;
    }

}

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

4.   文件上傳調(diào)用demo1.php實(shí)例:

<?php
require 'FUpload.php';

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

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


批改狀態(tài):合格

老師批語:你的命名很規(guī)范, 語義化很好
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報(bào)處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
作者最新博文
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)