Expand description
文件上传校验模块 — 对齐 PHP think\Validate 文件校验规则 + app\common\library\storage\Driver::validate
本模块实现文件类型/大小校验机制,对齐 PHP:
think\Validate::fileSize/fileExt/fileMime三个校验规则 (vendorframework/src/think/Validate.php第 957-1062 行)app\common\library\storage\Driver::validate业务层校验驱动 (第 25-46 行,默认规则 + 自定义消息)app\api\controller\file\Upload.php文件类型分类逻辑 (第 62-69 行,image/video/file 三类)
§PHP 对齐
§核心类映射
| PHP 类/方法 | Rust 结构/方法 | 说明 |
|---|---|---|
Driver::validate($name, $fileInfo, $sence) | FileValidator::validate_image | 图片校验驱动 |
validate([$name=>['fileSize'=>...]]) | FileValidateRule | 校验规则 |
$name.'.fileSize' => '最大可上传2M图片' | FileValidateMessages | 校验消息 |
Validate::fileExt() / checkExt() | FileValidator::check_ext | 扩展名校验 |
Validate::fileMime() / checkMime() | FileValidator::check_mime | MIME 校验 |
Validate::fileSize() / checkSize() | FileValidator::check_size | 大小校验 |
in_array($extension, [...]) 文件类型分类 | detect_file_type | 文件类型检测 |
§PHP 行为对齐(R5 硬约束)
- R5-9:
checkExt使用strtolower($file->extension())+in_array($ext, explode(',', $rule))(对齐 PHP Validate.php 第 957-964 行) - R5-10:
checkMime使用strtolower($file->getMime())+in_array($mime, explode(',', $rule))(对齐 PHP Validate.php 第 985-992 行) - R5-11:
checkSize使用$file->getSize() <= (int) $size(对齐 PHP Validate.php 第 973-976 行) - R5-12:
Driver::validate仅sence == 'image'执行校验 (对齐 PHP Driver.php 第 26 行) - R5-13:默认图片规则
fileSize=20971520, fileExt='jpg,jpeg,png,gif,bmp', fileMime='image/jpeg,image/png,image/gif,image/bmp'(对齐 PHP Driver.php 第 30-32 行) - R5-14:默认图片消息
'最大可上传2M图片'/'只能上传jpg,jpeg,png,gif,bmp格式图片'(对齐 PHP Driver.php 第 34-38 行) - R5-15:文件类型分类 image 12 种 / video 13 种 / file 其他 (对齐 PHP Upload.php 第 62-69 行)
§PHP 源码参考
e:\vue\test\鲜视达\server\vendor\topthink\framework\src\think\Validate.php- 第 957-964 行:
checkExt(File $file, $ext) - 第 973-976 行:
checkSize(File $file, $size) - 第 985-992 行:
checkMime(File $file, $mime) - 第 1002-1016 行:
fileExt($file, $rule) - 第 1025-1039 行:
fileMime($file, $rule) - 第 1048-1062 行:
fileSize($file, $rule)
- 第 957-964 行:
e:\vue\test\鲜视达\server\app\common\library\storage\Driver.php- 第 25-46 行:
validate($name, $fileInfo, $sence = 'image')
- 第 25-46 行:
e:\vue\test\鲜视达\server\app\api\controller\file\Upload.php- 第 62-69 行:文件类型分类逻辑
Structs§
- File
Validate Messages - 文件校验消息 — 对齐 PHP
Driver::validate第 34-38 行自定义消息 - File
Validate Rule - 文件校验规则 — 对齐 PHP
Driver::validate的fileSize/fileExt/fileMime规则 - File
Validator - 文件校验器 — 对齐 PHP
app\common\library\storage\Driver::validate
Enums§
- File
Type - 文件类型分类 — 对齐 PHP
app\api\controller\file\Upload.php第 62-69 行 - File
Validate Error - 文件校验错误 — 对齐 PHP
Driver::validate校验失败
Functions§
- detect_
file_ type - 检测文件类型 — 对齐 PHP
in_array($extension, [...])分类逻辑 - parse_
ext_ list - 解析扩展名列表 — 对齐 PHP
explode(',', $ext)+strtolower - parse_
mime_ list - 解析 MIME 列表 — 对齐 PHP
explode(',', $mime)+strtolower