首页> 实战笔录 >PHP开发笔记 >ThinkPHP ThinkPHP
TP6图片上传怎么验证图片的大小和类型?
作者:小萝卜 2020-09-14 【 TP6 】 浏览 3157
简介TP6图片上传怎么验证图片的大小和类型,官网提供了验证例子!
支持使用验证类对上传文件的验证,包括文件大小、文件类型和后缀:
public function upload(){
// 获取表单上传文件
$files = request()->file();
try {
validate(['image'=>'filesize:10240|fileExt:jpg|image:200,200,jpg'])
->check(['file'=>$files]);
$savename = [];
foreach($files as $file) {
$savename[] = \think\facade\Filesystem::putFile( 'topic', $file);
}
} catch (\think\exception\ValidateException $e) {
echo $e->getMessage();
}
}
验证参数 | 说明 |
---|---|
fileSize | 上传文件的最大字节 |
fileExt | 文件后缀,多个用逗号分割或者数组 |
fileMime | 文件MIME类型,多个用逗号分割或者数组 |
image | 验证图像文件的尺寸和类型 |
官网地址:https://www.kancloud.cn/manual/thinkphp6_0/1037639
很赞哦! (1)
相关文章
- TP5.1实现微信小程序用户登录后端代码分享
- tp5判断一张数据表是否存在
- TP5数据库操作时怎么过略不存在的字段
- TP6报错:Non-static method think\Request::controller() should not be called statically
- TP3.2输入验证和I方法使用
- TP6上传图片验证时报错think\\Validate::check() must be of the type array
- PHP/TP5使用supervisor/workerman在CLI模式下mysql断线重连解决办法
- thinkphp utf8中文排序,根据汉字拼音首字母排序
- url的301跳转 tp3.2 tp5.0实现方式
- tp5随机查询几条数据