废话不多说上代码
html的删除按键
<div class="layui-form-item">
<div class="layui-input-block">
<button type="reset" class="layui-btn" lay-submit lay-filter="clear" id="clear" >清空图片</button>
</div>
</div>
js
var id = document.getElementById("id").value;//获取要删除的id
$("#clear").click(function () {
$.ajax({
url:"{:url('admin/audit/clear')}",//写你要调转的方法
data:{id},
type:'post',
async: false,
success:function(res) {
if(res.err == 1) {
layer.msg(res.msg);//输出是否成功
window.location.reload();//页面刷新
} else {
layer.msg(res.msg);
}
}
})
return false;
});
记得引用sdk包,我其他文件有写上传的大家可以去看看
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;
public function clear()
{
if($this->request->isAjax()) {
$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
$a=Db::name('user')->where('id',$id)->field('id,qiniuimage')->find();
// 七牛文件名字
$url = $a['qiniuimage'];
//去掉末尾逗号,转二维数组
$k =array('in',rtrim($url, ","));
$keyarray=array();
for ($i=1; $i <count($k) ; $i++) {
$keyarray=str_replace('https://tu7.waimaimatou.com/','',$k[$i]);
}
$key=explode(',', $keyarray);
require_once APP_PATH . '/../vendor/qiniu/autoload.php';
$accessKey = config('accessKey');
$secretKey = config('secretKey');
// 构建鉴权对象
$auth = new Auth($accessKey, $secretKey);
// 要上传的空间
$bucket = config('bucket');
//初始化BucketManager
$bucketMgr = new BucketManager($auth);
//循环删除$bucket 中的文件 $key
$err=array();
for ($i=0; $i <count($key) ; $i++) {
$err=$bucketMgr->delete($bucket, $key[$i]);
}
//删除数据库的存放的地址
$b['qiniuimage']='';
Db::name('user')->where('id',$id)->update($b);
if ($err == null) {
return ["err"=>"1","msg"=>'删除成功',"data"=>""];
} else {
return ["err"=>"2","msg"=>"删除失败","data"=>""];
}
}
}