原文链接:http://blog.51cto.com/14210437/2357624
1.通过Composer安装七牛云PHP SDK,官方文档https://developer.qiniu.com/kodo/sdk/1283/javascript
2.后端核心代码如下:
use Qiniu\Auth;
//获取图片URL
public function qiniu_upload(){
set_time_limit(120);
ini_set('memory_limit','2000M');
$setting = C('UPLOAD_SITEIMG_QINIU');
$upload = new \Think\Upload($setting);// 实例化上传类
// $upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 11314572800 ;// 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg',"RM","RMVB","AVI","WMV","MPG ","MPEG","FLV","3GP","MP4","SWF","ASF","DIVX","XVID","3GP2","FLV1","MPEG1","MPEG2","MPEG3","MPEG4","H264",'rm','rmvb','avi','wmv','mpg','mpeg','flv','3gp','mp4','swf','asf','divx','xvid','3gp2','flv1','mpeg1','mpeg2','mpeg3','mpeg4','h264');// 设置附件上传类型
$upload->rootPath = './Uploads/file/'; // 设置附件上传根目录
$upload->savePath = 'lxsw/'; // 设置附件上传(子)目录
// 上传文件
$info = $upload->uploadOne($_FILES['file']);
// var_dump($info);exit;
// M('boss')->where('id='.$bid)->setInc('neicun_shiyong',$_FILES['logo']['size']);
if ($info){
$return = codemsg(3);
$return['data'] = $info['url'];
echo json_encode($return);exit;
}else{
$return = codemsg(-18);
$return['data'] = $upload->getError();
echo json_encode($return);exit;
}
}
3.前端上传组件使用plupload,官方下载地址https://www.plupload.com/
4.选择一款想用的demo,核心代码如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<!-- production -->
<script type="text/javascript" src="../js/plupload.full.min.js"></script>
<link rel="stylesheet" href="../js/jquery.plupload.queue/css/jquery.plupload.queue.css" type="text/css" media="screen" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" src="../js/plupload.full.min.js"></script>
<script type="text/javascript" src="../js/jquery.plupload.queue/jquery.plupload.queue.js"></script>
<div>
<label class="col-sm-2 col-sm-2 control-label">上传七牛云</label>
<div class="col-sm-10">
<div id="uploader" style="width: 60%;">
<p>Your browser doesn't have Flash, Silverlight or HTML5 support.</p>
</div>
</div>
</div>
</div>
<script type="text/javascript">
// $(function() {
// Setup html5 version
$("#uploader").pluploadQueue({
// General settings
runtimes : 'html5,flash,silverlight,html4',
url : 'http://www.lianxiang.com/Api/Test/qiniu_upload',//对应后端方法
chunk_size: '400mb',//分片上传
rename : true,
dragdrop: true,
filters : {
// Maximum file size
max_file_size : '500mb',
// Specify what files to browse for
mime_types: [
{title : "Image files", extensions : "jpg,gif,png,mp4"},
{title : "Zip files", extensions : "zip"}
]
},
// Resize images on clientside if we can
resize : {width : 320, height : 240, quality : 90},
flash_swf_url : '__PUBLIC__/js/Moxie.swf',//引入js路径
silverlight_xap_url : '__PUBLIC__/js/Moxie.xap',//引入js路径
init: {
FileUploaded:function(up,file,result){
console.log(result);
alert('上传成功');
}
}
});
</script>
转载于 https://blog.csdn.net/weixin_34228617/article/details/93033483