oop中 限制文件类型和大小

复制代码
 1 <?php
 2 /**
 3  * Created by IntelliJ IDEA.
 4  * User: jiabinwang
 5  * Date: 7/5/18
 6  * Time: 8:46 PM
 7  */
 8 
 9 namespace ElemeOpenApi\Efs;
10 
11 use ElemeOpenApi\Api\ContentService;
12 use ElemeOpenApi\Config\Config;
13 use ElemeOpenApi\Exception\BusinessException;
14 
15 class UploadVideoClient
16 {
17     private $config;
18     private $token;
19 
20     private $file_ext_list = array("MP4", "MOV");
21     private $file_max_size = 200;
22 
23     public function __construct($token, Config $config)
24     {
25         $this -> config = $config;
26         $this -> token = $token;
27     }
28 
29     /**
30      * 上传视频(封装ContentService中上传视频和获取efs配置接口)
31      *
32      * @param $file_path 视频文件本地地址
33      * @param $title 视频标题
34      * @param $desc 视频描述
35      * @param $video_type 视频类型
36      * @param $shop_id 店铺Id
37      * @return mixed
38      * @throws BusinessException 业务异常
39      */
40     public function upload_video_client($file_path, $title, $desc, $video_type, $shop_id) {
41         $file_extension = strtoupper(pathinfo(basename($file_path), PATHINFO_EXTENSION));
42 
43         if (!in_array($file_extension, $this -> file_ext_list)) {
44             throw new BusinessException("只支持mp4和mov格式的视频");
45         }
46 
47         if (filesize($file_path) > $this -> file_max_size * 1024 * 1024) {
48             throw new BusinessException("视频大小不能超过200M");
49         }
50 }
复制代码

 

posted @   HaimaBlog  阅读(175)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示