1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| <?php namespace app\validate;
use think\Validate;
class UploadValidate extends Validate { protected $rule = [ 'file' => 'require|file|fileSize:10485760|fileExt:jpg,png,gif,pdf,doc,docx,xls,xlsx,zip,rar', 'image' => 'require|file|fileSize:5242880|fileExt:jpg,png,gif|image:1920,1080', 'avatar' => 'require|file|fileSize:2097152|fileExt:jpg,png|image:500,500', 'document' => 'require|file|fileSize:20971520|fileExt:pdf,doc,docx,xls,xlsx,ppt,pptx', 'video' => 'require|file|fileSize:104857600|fileExt:mp4,avi,mov,wmv', 'audio' => 'require|file|fileSize:52428800|fileExt:mp3,wav,flac,aac' ]; protected $message = [ 'file.require' => '请选择要上传的文件', 'file.file' => '上传的不是有效文件', 'file.fileSize' => '文件大小不能超过10MB', 'file.fileExt' => '文件格式不支持', 'image.require' => '请选择要上传的图片', 'image.file' => '上传的不是有效图片文件', 'image.fileSize' => '图片大小不能超过5MB', 'image.fileExt' => '图片格式只支持jpg、png、gif', 'image.image' => '图片尺寸不能超过1920x1080', 'avatar.require' => '请选择头像图片', 'avatar.file' => '上传的不是有效图片文件', 'avatar.fileSize' => '头像大小不能超过2MB', 'avatar.fileExt' => '头像格式只支持jpg、png', 'avatar.image' => '头像尺寸不能超过500x500', 'document.require' => '请选择要上传的文档', 'document.file' => '上传的不是有效文档文件', 'document.fileSize' => '文档大小不能超过20MB', 'document.fileExt' => '文档格式不支持', 'video.require' => '请选择要上传的视频', 'video.file' => '上传的不是有效视频文件', 'video.fileSize' => '视频大小不能超过100MB', 'video.fileExt' => '视频格式不支持', 'audio.require' => '请选择要上传的音频', 'audio.file' => '上传的不是有效音频文件', 'audio.fileSize' => '音频大小不能超过50MB', 'audio.fileExt' => '音频格式不支持' ]; protected $scene = [ 'image' => ['image'], 'avatar' => ['avatar'], 'document' => ['document'], 'video' => ['video'], 'audio' => ['audio'] ];
protected function checkFileType($value, $rule, array $data = []) { if (!$value instanceof \think\File) { return '上传的不是有效文件'; } $allowedTypes = [ 'image/jpeg', 'image/png', 'image/gif', 'application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ]; if (!in_array($value->getMime(), $allowedTypes)) { return '文件类型不被允许'; } return true; }
protected function checkFileSecurity($value, $rule, array $data = []) { if (!$value instanceof \think\File) { return '上传的不是有效文件'; } $content = file_get_contents($value->getPathname()); if (strpos($content, '<?php') !== false || strpos($content, '<?=') !== false) { return '文件包含不安全内容'; } if (strpos($content, '<script') !== false) { return '文件包含不安全脚本'; } return true; } }
|