先在七牛云申请账户及accessKey信息,保存为数组文件,方便后期调用
// +----------------------------------------------------------------------
// | 七牛云设置
// +----------------------------------------------------------------------
//七牛云appid
'qn_accessKey' => 'idaqr1_NoVpXmz...',
//七牛云appSecret
'qn_secretKey' => 'y6J4dIkqHC...',
//七牛储存外链地址;
'qn_url' => 'http://heart.***.net/',
//七牛存储空间列表;
'qn_bucket' => 'heart',
//七牛私有队列;
'qn_pipeline' => 'chalu',
开发文档及下载地址参见:https://developer.qiniu.com/kodo/sdk/1241/php
<div class="weui-form__title" style="margin: 10px auto;text-align: center;">
<button class="layui-btn" id="test3"><i class="layui-icon">i>上传图片button>
<button class="layui-btn" id="test5" style="margin: auto 10px;"><i class="layui-icon">i>上传视频button>
div>
//上传视频至七牛云;
upload.render({
elem: '#test5'
, url: 'safe.php?m=Safe&a=safeUpVideo&act=upVideo&safe_id=' + safe_id
, accept: 'video' //视频
, exts: 'mp4|mpg|mov' //只允许上传压缩文件
, before: function () {
layer.load();
}
, done: function (res) {
if (res.status != "0") {
return layer.msg('上传失败!');
}
//上传成功
if (res.status == "0") {
layer.alert(res.err, {icon: 6}, function () {
// 获得frame索引
var index = parent.layer.getFrameIndex(window.name);
//关闭当前frame
parent.layer.close(index);
//重新加载页面
window.parent.location.reload();
});
}
}
});
});
//引入入口文件,并写好相应要使用的模块命名空间
if (!defined('QINIU_ROOT')) {
define('QINIU_ROOT', dirname(__FILE__) . '/');
require(QINIU_ROOT . 'qiniu/autoload.php');
}
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;
class QINIU
{
public function upchange($conf_accessKey, $conf_secretKey, $conf_bucket, $conf_pipeline, $filePath, $mediaid)
{
//初始化好Auth 类,用于签名的公钥和私钥
$accessKey = $conf_accessKey;
$secretKey = $conf_secretKey;
$auth = new Auth($accessKey, $secretKey);
$bucket = $conf_bucket;
//数据处理队列名称,不设置代表不使用私有队列,使用公有队列。
$pipeline = $conf_pipeline;
//通过添加'|saveas'参数,指定处理后的文件保存的bucket和key
//不指定默认保存在当前空间,bucket为目标空间,后一个参数为转码之后文件名
$savekey = Qiniu\base64_urlSafeEncode($bucket . ':' . $mediaid . '.mp4');
//设置转码参数
$fops = "avthumb/mp4/ab/128k/ar/44100/acodec/libfaac/r/30/vb/720k/vcodec/libx264/s/640x360/autoscale/1/stripmeta/0";
$fops = $fops . '|saveas/' . $savekey;
if (!empty($pipeline)) { //使用私有队列
$policy = array(
'persistentOps' => $fops,
'persistentPipeline' => $pipeline
);
} else { //使用公有队列
$policy = array(
'persistentOps' => $fops
);
}
//指定上传转码命令;
$uptoken = $auth->uploadToken($bucket, null, 3600, $policy);
$key = $mediaid . '.mp4'; //七牛云中保存的文件名
$uploadMgr = new UploadManager();
//上传文件并转码$filePath为本地文件路径;
list($ret, $err) = $uploadMgr->putFile($uptoken, $key, $filePath);
if ($err !== null) {
return false;
} else {
return $ret['key'];
}
}
}
//上传视频 by poleng 2020-09-05
case "upVideo";
$safe_id = get_param("safe_id");
$file = $_FILES['file'];
//处理文件后缀名;
$ext = explode('.', $_FILES['file']['name']);
$ext = end($ext);//mp4
//上传至七牛云并转码;
require 'libs/QiNiu.php';
$qiniu = new QINIU;
//文件随机命名;
$mediaId = md5(time());
$fileName = $file['tmp_name'];
//执行上传操作;
$qiniu->upchange($CONF['qn_accessKey'], $CONF['qn_secretKey'], $CONF['qn_bucket'], $CONF['qn_pipeline'], $fileName, $mediaId);
//获取上传七牛云的文件名;
$videoUrl = $CONF['qn_url'] . $mediaId . "." . $ext;
//视频入库;
$up_preview = "upload/video.jpg";
$up_tpye = "1";
$up_time = time();
$db->insert('upfiles', array('safe_id' => $safe_id, 'up_url' => $videoUrl, 'up_preview' => $up_preview, 'up_tpye' => $up_tpye, 'up_time' => $up_time));
//返回JSON;
$res['status'] = "0";
$res['videoUrl'] = $videoUrl;
$res['err'] = '上传成功!';
die(json_encode_lockdata($res));
break;
Done!