public function upload(){
if(intval(session(“userid”))==0)
{
header(‘Content-type: text/json’);
header(‘HTTP/1.1 401 error’);
echo json_encode(array(“success” => false,”code”=> 1001,”msg” => “请先登录!”,”obj”=> null,”map”=> null,”list”=> null));
exit;
}
if(I(‘get.fileType’,0)==2){
$ftpconf = C(‘UPLOAD_VIDEO_TYPE_CONFIG’);//上传到音频服务器
}
else{
$ftpconf = C(‘UPLOAD_TYPE_CONFIG’);//上传到图片服务器
}
if(I(‘get.fileType’,0)==2)
{
$upload->exts = array(‘mp3’);// 设置附件上传类型
$upload->savePath = date(‘Y’,time()).’/’.date(‘m’,time()).’/’.date(‘d’,time()).’/’.session(“userid”).’/’; // 设置附件上传(子)目录
$upload->subName = ”;
}
if(I(‘get.fileType’,0)!=2){
if(I(‘get.fileType’,0)==0){
$savePath=”backImg/”;//背景图
}else{
$savePath=”pic/”;//封面图
}
//*********上传本地背景图
$upload2 = new \Think\Upload();// 实例化上传类
$upload2->maxSize = 3145728 ;// 设置附件上传大小
$upload2->exts = array(‘jpg’, ‘gif’, ‘png’, ‘jpeg’);
$upload2->rootPath = ‘./Uploads/’;
$upload2->savePath = $savePath.session(“userid”).’/’;
$upload2->subName = array(‘date’,’Ym’);
$upload2->saveName = ‘uniqid’;
$info2 = $upload2->upload();
//***ftp背景图
$upload = new \Think\Upload($ftpconf,”,$ftpconf);
$upload->maxSize = 3145728 ;
$upload->exts = array(‘jpg’, ‘gif’, ‘png’, ‘jpeg’);// 设置附件上传类型
$upload->subName = array(‘date’,’Ym’);
$upload->savePath = ‘h5/’.$savePath.session(“userid”).’/’;
$upload->rootPath = $ftpconf[‘rootPath’];
$upload->saveName = ‘uniqid’;
// 采用GUID序列命名 $upload->saveName = ‘guid’;
$info = $upload->upload(); //上传到FTP图片服务器
}else{
//将音乐上传到本地
$upload2 = new \Think\Upload(array(),’LOCAL’);// 实例化上传类
$upload2->maxSize = 3145728 ;// 设置附件上传大小
$upload2->exts = array(‘mp3’);// 设置附件上传类型
$upload2->savePath = date(‘Y’,time()).’/’.date(‘m’,time()).’/’.date(‘d’,time()).’/’.session(“userid”).’/’; // 设置附件上传(子)目录
$upload2->rootPath = ‘./Uploads/’; // 设置附件上传根目录
$upload2->subName = ”;
$fileftpname = str_replace(‘.’.$info[‘file’][‘ext’],”,$info[‘file’][‘savename’]);
$upload2->saveName = $fileftpname;
$info2 = $upload2->upload();
}
if(!$info || !$info2) {// 上传错误提示错误信息
header(‘Content-type: text/json’);
header(‘HTTP/1.1 401 error’);
echo json_encode(array(“success” => false,”code”=> 1001,”msg” => “文件上传错误!”,”obj”=> null,”map”=> null,”list”=> null));
$this->error($upload->getError());
exit;
}else{// 上传成功 获取上传文件信息
header(‘Content-type: text/json’);
header(‘HTTP/1.1 200 ok’);
if(I(‘get.fileType’,0)!=2){
$info = $info2;//生成缩略图必须是本地文件
}
foreach($info as $file){
if(I(‘get.fileType’,0)!=2)
{
$image = new \Think\Image();
$thubimage = $upload2->rootPath.$file[‘savepath’].$file[‘savename’];
//缩略图名字
$thubimagename = str_replace(“.”.$file[‘ext’],”_thumb.”.$file[‘ext’],$file[‘savename’]);
//本地缩略图/Uploads/pic/1/201603/532_thumb.jpg
$thubimagenew =$upload2->rootPath.$file[‘savepath’].$thubimagename;
$image->open($thubimage);
if(I(‘get.fileType’,0)==0)
{
$image->thumb(80, 126)->save($thubimagenew);
}
else
{
$image->thumb(80, 80)->save($thubimagenew);
}
//将生成的缩略图上传到图片服务器
$ftpuser = $ftpconf[‘username’];
$ftppass = $ftpconf[‘password’];
$ftphost = $ftpconf[‘host’];
$ftpport = $ftpconf[‘port’];
$rootPath = $ftpconf[‘rootPath’];
$ftptimeout = $ftpconf[‘timeout’];
//链接FTP服务器
$link = ftp_connect($ftphost, $ftpport, $ftptimeout);
//ftp保存缩略图地址
$ftpthubimagenew=str_replace(“Uploads”,”h5″, $thubimagenew);
if($link){
ftp_login($link, $ftpuser, $ftppass);
$putinfo = ftp_put($link,$ftpthubimagenew,$thubimagenew,FTP_BINARY);
if (!$putinfo){
echo ‘{“success”:false,”code”:101,”msg”:”操作失败”,”obj”:null,”map”:null,”list”:null}’;
}
}
ftp_close($link);
}
$sizeint = intval($file[‘size’]/1024);
$jsonstr = ‘{“success”:true,”code”:200,”msg”:”success”,”obj”:{“id”:9386090,”name”:”‘.$file[‘savename’].'”,”extName”:”‘.strtoupper($file[‘ext’]).'”,”fileType”:0,”bizType”:0,”path”:”‘.$file[‘savepath’].$file[‘savename’].'”,”tmbPath”:”‘.$thubimagenew.'”,”createTime”:1426209412922,”createUser”:”‘.session(“userid”).'”,”sort”:0,”size”:’.$sizeint.’,”status”:1},”map”:null,”list”:null}’;
$model = M(‘upfile’);
// 取得成功上传的文件信息
// 保存当前数据对象
$data[‘ext_varchar’] = strtoupper($file[‘ext’]);
$data[‘filename_varchar’] = $file[‘name’];
$data[‘filetype_int’] = I(‘get.fileType’,0);
$data[‘biztype_int’] = I(‘get.bizType’,0);
$data[‘userid_int’] = session(“userid”);
$data[‘filesrc_varchar’] = $file[‘savepath’].$file[‘savename’];
$data[‘sizekb_int’] = $sizeint;
if(I(‘get.fileType’,0)==1)//封面图路径字段
$thubimagenew=$file[‘savepath’].$thubimagename;
$data[‘filethumbsrc_varchar’] =$thubimagenew;
$data[‘create_time’] = date(‘y-m-d H:i:s’,time());
$model->add($data);
echo $jsonstr;
}
}
}
转载于:https://my.oschina.net/ndczds/blog/639032