我这里加了 封面图片 可以不理睬! 我没有存oss.
阿里的OSS 自己可以去官网下载 我这里放到 Vendor 下的.
1 # 注意这里OSS中 请设置 存储空间名称为公共的 才能直接使用这里返回的地址进行播放 不然 需要加密呗
2 use OSS\Core\OssException; # 必须
3 use OSS\OssClient; # 必须
4 class ClasstimeAction extends CommonAction
5 {
6 public function add_sub()
7 {
8 header('Content-type:text/html,charset:ust-8');
9 $file = $_FILES['file'];
10 $filTtype = $file['name'];//文件名
11 $fileName = substr($filTtype,strrpos($filTtype,'.'));
12 $filetype = array('.jpg', '.jpeg', '.gif', '.bmp', '.png');
13 $str = rand(1,99999);
14 $useName = date('Ymd').$str;
15 $picNewName = $useName.$fileName;
16 $upload_path = "../Public/Rbac/file/";
17 if (!in_array($fileName, $filetype))
18 {
19 $this->success("图片格式错误","__APP__/Public/index");
20 exit;
21 }
22 if(!is_uploaded_file($file['tmp_name'])){
23 $this->success("请使用正确途径上传图片","__APP__/Public/index");
24 exit;
25 }
26 if(!move_uploaded_file($file['tmp_name'],$upload_path.$picNewName)){
27 $this->success("图片存储失败 请重新上传","__APP__/Public/index");
28 exit;
29 }
30
31
32 #视频
33 $video = $_FILES['video'];
34 $videoTtype = $video['name'];//文件名
35 $videoName = substr($videoTtype,strrpos($videoTtype,'.'));
36 $videotype = array('.mp4'); // 自定 我这随便写一个
37 $str = rand(1,99999);
38 $useName = date('Ymd').$str;
39 $videoNewName = $useName.$videoName;
40 $upload_path = "../Public/Rbac/video/";
41 if (in_array($videoName, $videotype)) {
42 if (is_uploaded_file($video['tmp_name'])) {
43 if (move_uploaded_file($video['tmp_name'],$upload_path.$videoNewName)) {
44
45 vendor('OSS.autoload'); // 引入 oss上传类
46 $accessKeyId = "xxxxxxxxxx"; //去阿里云后台获取秘钥
47 $accessKeySecret = "xxxxxxxxxx"; //去阿里云后台获取秘钥
48 $endpoint = "xxxxxxxxxx"; //你的阿里云OSS地址
49
50 $object = date('Y-m-d').'/'.$videoTtype; //想要保存文件的名称
51 $file = $upload_path.$videoNewName; //文件路径,必须是本地的。
52 $bucket = "xxxxxxxxxx"; // 存储空间。
53
54 try{
55 $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
56 $getOssInfo = $ossClient->uploadFile($bucket, $object, $file);
57 if($getOssInfo['info']['http_code'] == 200){
58 unlink($file); # 上传成功删除本地文件
59 $curl = $getOssInfo['info']['url'];
60 # 添加数据库
61 $time = $_POST['classsc'].':'.$_POST['classsc1'];
62 $data['addresspic'] = $picNewName;
63 $data['addressvideo'] = $curl;
64 $data['classtime'] = $_POST['classtime'];
65 $data['classjj'] = $_POST['classjj'];
66 $data['classsc'] = $time;
67 $data['cid'] = $_REQUEST['code'];
68 $data['type'] = $_REQUEST['testSee'];
69 $data['syrenqun'] = $_REQUEST['classshiyin'];
70 $data['status'] = 1;
71 $data['ctime'] = time();
72 $data['user'] = $_SESSION['name'];
73 D("classtime")->add($data);
74 $this->success("操作成功");
75 }else{
76 $this->error("上传OSS异常");
77 }
78 // 抛出异常
79 } catch(OssException $e) {
80 printf(__FUNCTION__ . ": FAILED\n");
81 printf($e->getMessage() . "\n");
82 return;
83 }
84
85 }else{
86 $this->success("视频存储失败 请重新上传","__APP__/Public/index");
87 exit;
88 }
89 }else{
90 $this->success("请使用正确途径上传视频","__APP__/Public/index");
91 exit;
92 }
93 }else{
94 $this->success("请上传正确的视频格式 支持格式(mp4)","__APP__/Public/index");
95 exit;
96 }
97 }
98 }
99
100
101