您好,登錄后才能下訂單哦!
1.通過Composer安裝七牛云PHP SDK,官方文檔https://developer.qiniu.com/kodo/sdk/1283/javascript
2.后端核心代碼如下:
use Qiniu\Auth as Auth;
use Qiniu\Config;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
......
......
//上傳七牛云
function qiniu_upload(){
set_time_limit(120);
ini_set('memory_limit','2000M');
if(!empty($_FILES['file']))
{
$file = request()->file('file');
// 要上傳圖片的本地路徑
$filePath = $file->getRealPath();
$ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION); //后綴
//獲取當前控制器名稱
$controllerName = 'index';
// 上傳到七牛后保存的文件名
$key =substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
// 需要填寫你的 Access Key 和 Secret Key
$accessKey = config('ACCESSKEY');
$secretKey = config('SECRETKEY');
// 構建鑒權對象
$auth = new Auth($accessKey, $secretKey);
// 要上傳的空間
$bucket = config('BUCKET');
$domain = config('DOMAINImage');
$token = $auth->uploadToken($bucket);
// 初始化 UploadManager 對象并進行文件的上傳
$uploadMgr = new UploadManager();
// 調用 UploadManager 的 putFile 方法進行文件的上傳
list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
if ($err !== null) {
retErr($err);
} else {
//返回圖片的完整URL
$url = $domain.$ret['key'];
echo $url;
}
}
}
3.前端上傳組件使用plupload,官方下載地址https://www.plupload.com/
4.選擇一款想用的demo,核心代碼如下:
......
......
<link rel="stylesheet" href="__PUBLIC__/plupload/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="__PUBLIC__/plupload/js/plupload.full.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/plupload/js/jquery.plupload.queue/jquery.plupload.queue.js"></script>
......
......
<label class="col-sm-2 col-sm-2 control-label">上傳七牛云</label>
<div class="col-sm-10">
<div id="uploader" >
<p>Your browser doesn't have Flash, Silverlight or HTML5 support.</p>
</div>
</div>
</div>
......
......
$(function() {
// Setup html5 version
$("#uploader").pluploadQueue({
// General settings
runtimes : 'html5,flash,silverlight,html4',
url : '{:url("xxx/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){
layer.msg('上傳成功');
$('#url').val(result.response);//后端返回的上傳文件路徑
}
}
});
5.服務器配置參數
添加nginx3個參數:請求時間
fastcgi_connect_timeout 120s;
fastcgi_send_timeout 120s;
fastcgi_read_timeout 120s;
修改php.ini 3個參數:文件大小
memory_limit = 300M
post_max_size = 300M
upload_max_filesize =300M
6.實現結果
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。