AssetController.class.php
5.0 KB
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
120
121
<?php
namespace Asset\Controller;
use Common\Controller\AdminbaseController;
class AssetController extends AdminbaseController {
function _initialize() {
$adminid=sp_get_current_admin_id();
$userid=sp_get_current_userid();
if(empty($adminid) && empty($userid)){
exit("非法上传!");
}
}
// 文件上传
public function plupload(){
$upload_setting=sp_get_upload_setting();
$filetypes=array(
'image'=>array('title'=>'Image files','extensions'=>$upload_setting['image']['extensions']),
'video'=>array('title'=>'Video files','extensions'=>$upload_setting['video']['extensions']),
'audio'=>array('title'=>'Audio files','extensions'=>$upload_setting['audio']['extensions']),
'file'=>array('title'=>'Custom files','extensions'=>$upload_setting['file']['extensions'])
);
$image_extensions=explode(',', $upload_setting['image']['extensions']);
if (IS_POST) {
$all_allowed_exts=array();
foreach ($filetypes as $mfiletype){
array_push($all_allowed_exts, $mfiletype['extensions']);
}
$all_allowed_exts=implode(',', $all_allowed_exts);
$all_allowed_exts=explode(',', $all_allowed_exts);
$all_allowed_exts=array_unique($all_allowed_exts);
$file_extension=sp_get_file_extension($_FILES['file']['name']);
$upload_max_filesize=$upload_setting['upload_max_filesize'][$file_extension];
$upload_max_filesize=empty($upload_max_filesize)?2097152:$upload_max_filesize;//默认2M
$app=I('post.app/s','');
if(!in_array($app, C('MODULE_ALLOW_LIST'))){
$app='default';
}else{
$app= strtolower($app);
}
$savepath=$app.'/'.date('Ymd').'/';
//上传处理类
$config=array(
'rootPath' => './'.C("UPLOADPATH"),
'savePath' => $savepath,
'maxSize' => $upload_max_filesize,
'saveName' => array('uniqid',''),
'exts' => $all_allowed_exts,
'autoSub' => false,
);
$upload = new \Think\Upload($config);//
$info=$upload->upload();
//开始上传
if ($info) {
//上传成功
$oriName = $_FILES['file']['name'];
//写入附件数据库信息
$first=array_shift($info);
if(!empty($first['url'])){
$url=$first['url'];
$storage_setting=sp_get_cmf_settings('storage');
$qiniu_setting=$storage_setting['Qiniu']['setting'];
$url=preg_replace('/^https/', $qiniu_setting['protocol'], $url);
$url=preg_replace('/^http/', $qiniu_setting['protocol'], $url);
$preview_url=$url;
if(in_array($file_extension, $image_extensions)){
if(C('FILE_UPLOAD_TYPE')=='Qiniu' && $qiniu_setting['enable_picture_protect']){
$preview_url = $url.$qiniu_setting['style_separator'].$qiniu_setting['styles']['thumbnail300x300'];
$url= $url.$qiniu_setting['style_separator'].$qiniu_setting['styles']['watermark'];
}
}else{
$preview_url='';
$url=sp_get_file_download_url($first['savepath'].$first['savename'],3600*24*365*50);//过期时间设置为50年
}
}else{
$url=C("TMPL_PARSE_STRING.__UPLOAD__").$savepath.$first['savename'];
$preview_url=$url;
}
$filepath = $savepath.$first['savename'];
$this->ajaxReturn(array('preview_url'=>$preview_url,'filepath'=>$filepath,'url'=>$url,'name'=>$oriName,'status'=>1,'message'=>'success'));
} else {
$this->ajaxReturn(array('name'=>'','status'=>0,'message'=>$upload->getError()));
}
} else {
$filetype = I('get.filetype/s','image');
$mime_type=array();
if(array_key_exists($filetype, $filetypes)){
$mime_type=$filetypes[$filetype];
}else{
$this->error('上传文件类型配置错误!');
}
$multi=I('get.multi',0,'intval');
$app=I('get.app/s','');
$upload_max_filesize=$upload_setting[$filetype]['upload_max_filesize'];
$this->assign('extensions',$upload_setting[$filetype]['extensions']);
$this->assign('upload_max_filesize',$upload_max_filesize);
$this->assign('upload_max_filesize_mb',intval($upload_max_filesize/1024));
$this->assign('mime_type',json_encode($mime_type));
$this->assign('multi',$multi);
$this->assign('app',$app);
$this->display(':plupload');
}
}
}