Qiniu文件改名放到v开头的文件夹里
use think\Config;
vendor('Qiniu.autoload');
use think\Request;
use Qiniu\Auth as Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
// 图片修改
public function upload()
{
$id = $_REQUEST['id'];
$file = request()->file('image');
// 要上传图片的本地路径
$filePath = $file->getRealPath();
$ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION); //后缀
// 上传到七牛后保存的文件名
$key = substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;
// 基本上传配置
$accessKey = Config::get('qiniu.accessKey');
$secretKey = Config::get('qiniu.secretKey');
// 构建鉴权对象
$auth = new Auth($accessKey, $secretKey);
$bucket = Config::get('qiniu.bucket');
$domain = Config::get('qiniu.DOMAIN');
$token = $auth->uploadToken($bucket);
// 初始化 UploadManager 对象并进行文件的上传
$uploadMgr = new UploadManager();
// 调用 UploadManager 的 putFile 方法进行文件的上传
list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
if ($err !== null) {
return json(["code" => 1, "msg" => $err, "data" => ""]);
} else {
//返回图片的完整URL
$shopId = input('post.shopId');
$data = db('details_of_the_group_tour')->where('id',$id)->update(['image' =>'http://'.$domain . '/' . $ret['key']]);
if ($data) {
$this->success('修改成功');
} else {
$this->error('修改失败');
}
}
}
来源:CSDN
作者:qq_39476250
链接:https://blog.csdn.net/qq_39476250/article/details/103582529