FileValidate.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\library\storage;
  13. /**
  14. * 文件上传验证类
  15. * Class FileValidate
  16. * @package app\common\library\storage
  17. */
  18. class FileValidate extends \think\Validate
  19. {
  20. // 验证规则
  21. protected $rule = [
  22. // 图片文件: jpg,jpeg,png,bmp,gif
  23. // 文件大小: 2MB = (1024 * 1024 * 2) = 2097152 字节
  24. 'image' => 'filesize:2097152|fileExt:jpg,jpeg,png,bmp,gif',
  25. // 视频文件: mp4
  26. // 文件大小: 10MB = (1024 * 1024 * 10) = 10485760 字节
  27. // 文件大小: 50MB = (1024 * 1024 * 50) = 52428800 字节
  28. 'video' => 'filesize:52428800|fileExt:mp4,mov,flv,mpeg',
  29. 'file' => 'filesize:10485760|fileExt:txt,xls',
  30. ];
  31. // 错误提示信息
  32. protected $message = [
  33. 'image.filesize' => '图片文件大小不能超出2MB',
  34. 'image.fileExt' => '图片文件扩展名有误',
  35. 'video.filesize' => '视频文件大小不能超出50MB',
  36. 'video.fileExt' => '视频文件扩展名有误',
  37. 'file.filesize' => '文件大小不能超出10MB',
  38. 'file.fileExt' => '文件扩展名有误',
  39. ];
  40. // 验证场景
  41. protected $scene = [
  42. 'image' => ['image'],
  43. 'video' => ['video'],
  44. ];
  45. }