1,百度编辑器中使用七牛云进行上传案例,根据网上案例进行接入tp5,经过小修改后实测没问题
目录如图
配置如下图
资源下载:https://download.csdn.net/download/itxiaolong3/15533263
2,接口使用七牛云案例,网上去下载七牛云php版的sdk放到extend目录下,命名为qiniu
第一步:在config中配置号七牛云信息
第二步:方法中使用
public function file_qiniu(){
$file = request()->file('file');
// 要上传图片的本地路径
$filePath = $file->getRealPath();
$ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION); //后缀
// 上传到七牛后保存的文件名
$key =date('YmdHis') . rand(0, 9999) . '.' . $ext;
// 需要填写你的 Access Key 和 Secret Key
// 构建鉴权对象
$accessKey =config("qiniu")["accessKey"];
$secretKey =config("qiniu")["secretKey"];
$auth=new Auth($accessKey,$secretKey);
// 要上传的空间
$bucket =config("qiniu")["bucket"];
//域名
$domain=config("qiniu")["domain"];
$token = $auth->uploadToken($bucket);
// 初始化 UploadManager 对象并进行文件的上传
$uploadMgr = new UploadManager();
// 调用 UploadManager 的 putFile 方法进行文件的上传
list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
if ($err !== null) {
return ["err"=>1,"msg"=>$err,"data"=>""];
} else {
//返回图片的完整URL
$imgPath='http://'.$domain.'/'.$key;
//赋值
$data["thumb_url"] = $imgPath;
return json(["code"=> 1, "msg" => "上传成功", "url" =>$imgPath]);
}
}
//删除七牛云图片
function delfromqiniu() {
$src = str_replace(ROOT_PATH . '/', '', str_replace('//', '/', input('src')));
$imginfo=explode('/',$src);
$delFileName=$imginfo[count($imginfo)-1];
if( $delFileName ==null){
return json(["code"=>0,"msg"=>"删除失败,参数不正确",'url'=>'|'.input('src')]);
}
// 构建鉴权对象
$accessKey =config("qiniu")["accessKey"];
$secretKey =config("qiniu")["secretKey"];
$auth=new Auth($accessKey,$secretKey);
// 要上传的空间
$bucket =config("qiniu")["bucket"];
// 配置
$config = new \Qiniu\Config();
// 管理资源
$bucketManager=new BucketManager($auth, $config);
// 删除文件操作
$res = $bucketManager->delete(config("qiniu.bucket"), $delFileName);
if (is_null($res)) {
return json(["code"=>1,"msg"=>"删除成功",'url'=>'']);
}else{
return json(["code"=>0,"msg"=>"删除失败,图片找不到",'url'=>'']);
}
}
如果报错Class 'Qiniu\Auth' not found
在tp5入口文件index.php中加入自动引入文件,如下图