tp6針對thinkphp6.0 的文件上傳驗證失效自己寫的上傳插件

use think\image;

需要think-image支持,
安裝:composer require topthink/think-image
在這裏插入圖片描述
tp6官方的上傳返回的圖片格式中有\這個斜槓,linux系統好像不識別它。
驗證的rule格式,目前只寫了圖片類型的,其它的文件類型要自己增加。
要允許的參數比較多的話 使用逗號連接

$rule = [
             'rule' => [
                 'mimeType' => 'jpg,png,JPG,JPEG,PNG,jpeg',
                 'size' => '2',
                 'image' => '1000,1000',
                 'suffix' => 'jpg,png'
             ],
             'msg' => [
                 'mimeType' => '圖片格式錯誤',
                 'size' => '圖片大小超過4MB',
                 'image' => '圖片尺寸不符合規範',
                 'suffix' => '圖片後綴錯誤'
             ]
         ];
handleUploadFile($file, $rule = null, $type)

這個函數傳入三個屬性,
$file 是單個文件上傳,獲取的文件對象
$rule 是下面圖片中的
在這裏插入圖片描述

<?php
/*
*  $files = request()->file('image');
*/

namespace thinkFile;

use think\image;

class thinkFile
{
    /**
     * 檢查上傳文件的合法性
     *
     * @param $file
     * @param $rule
     * @return array
     */
    public function handleCheck($file, $rule)
    {
        $rule_d = $rule['rule'];
        foreach ($rule_d as $key => $val) {
            switch ($key) {
                case 'suffix':
                    $nameArr = explode('.', $file->getOriginalName());
                    $name = $nameArr[count($nameArr) - 1];
                    if (strpos($val, $name) !== false) {

                    } else {
                        return ['states' => false, 'msg' => $rule['msg'][$key]];
                    }
                    break;
                case 'mimeType':
                    $type = explode('/', $file->getMime())[1];
                    if (strpos($val, $type) !== false) {

                    } else {
                        return ['states' => false, 'msg' => $rule['msg'][$key]];
                    }
                    break;
                case 'size':
                    $size = (int)$file->getSize() / 1024000;
                    if ($size > $val) {
                        return ['states' => false, 'msg' => $rule['msg'][$key]];
                    }
                    break;
                case 'image':
                    $image = Image::open($file->getRealPath());
                    $width = $image->width();
                    $height = $image->height();
                    $image = explode(',', $val);
                    if ($width > $image[0] || $height > $image[1]) {
                        return ['states' => false, 'msg' => $rule['msg'][$key]];
                    }
                    break;
                default:
                    $pice_path = '/runtime/storage/other/';
                    break;
            }
        }
        return ['states' => true, 'msg' => '效驗正常'];
    }

    /**
     * 按照類別上傳文件到指定的目錄
     *
     * @param $file
     * @param null $rule
     * @param $type
     * @return array
     * @throws \think\Exception
     */
    public function handleUploadFile($file, $rule = null, $type)
    {
        $base_path = app()->getRootPath();
        switch ($type) {
            case 'view':
                $pice_path = '/public/static/uploads/image/';
                break;
            case 'image':
                $pice_path = '/runtime/storage/image/';
                break;
            case 'zip':
                $pice_path = '/runtime/storage/zip/';
                break;
            case 'video':
                $pice_path = '/runtime/storage/video/';
                break;
            case 'word':
                $pice_path = '/runtime/storage/word/';
                break;
            default:
                $pice_path = '/runtime/storage/other/';
                break;
        }

        if ($rule == null) {
            $status = ['states' => true, 'msg' => '無需效驗'];
        } else {
            $status = $this->handleCheck($file, $rule);
        }

        if ($status['states'] == true) {
            $type = explode('/', $file->getMime())[1];
            $image_name = md5(date('Ymd-His') . mt_rand(1, 99999)) . "." . $type;
            if (!file_exists($base_path . $pice_path)) {
                mkdir($base_path . $pice_path, 0700);
            }
            $image_file_path = $pice_path . date('Ymd');
            $image_file = $base_path . $image_file_path;
            $imge_real_url = $image_file . '/' . $image_name;
            $imge_web_url = $image_file_path . '/' . $image_name;
            if (!file_exists($image_file)) {
                mkdir($image_file, 0700);
//                fopen($image_file . '/' . $image_name, "w");
            }
            $real_path = $file->getRealPath();
            if (move_uploaded_file($real_path, $imge_real_url)) {
                return ['code' => 0, 'data' => $imge_web_url, 'msg' => 'success', 'word' => '文件保存成功'];
            } else {
                return ['code' => 1, 'data' => null, 'msg' => 'error', 'word' => '上傳失敗'];
            }
        } else {
            throw new \think\Exception($status['msg']);
        }
    }
}

如何使用?

try {
        $thinkFile = new thinkFile();
        $imge_web_url = $thinkFile->handleUploadFile($file, [
            'rule' => [
                'mimeType' => 'jpg,png,JPG,JPEG,PNG,jpeg',
                'size' => '5',
                'image' => '5000,5000',
                'suffix' => 'jpg,png'
            ],
            'msg' => [
                'mimeType' => '圖片格式錯誤',
                'size' => '圖片大小超過5MB',
                'image' => '圖片尺寸不符合規範',
                'suffix' => '圖片後綴錯誤'
            ]
        ], 'image');
        return json(['code' => 0, 'data' => $imge_web_url['data'], 'msg' => 'success', 'word' => '上傳完成']);
    } catch (\think\Exception $e) {
        return json(['code' => 1, 'data' => null, 'msg' => 'error', 'word' => $e->getMessage()]);
    }

測試:
png上傳
在這裏插入圖片描述
測試gif上傳
在這裏插入圖片描述
測試 尺寸超過設定值
在這裏插入圖片描述

發佈了87 篇原創文章 · 獲贊 14 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章