1. 先去七牛云官网下载sdk
2.将sdk解压放到vendor
vendor('Qiniu.autoload'); use Qiniu\Auth as Auth; use Qiniu\Storage\BucketManager; use Qiniu\Storage\UploadManager;
public function ceshi1() { $file = request()->file('image'); // 要上传图片的本地路径 $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('DOMAIN'); $token = $auth->uploadToken($bucket); // 初始化 UploadManager 对象并进行文件的上传 $uploadMgr = new UploadManager(); // 调用 UploadManager 的 putFile 方法进行文件的上传 list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath); if ($err !== null) { echo ["err"=>1,"msg"=>$err,"data"=>""]; } else { //返回图片的完整URL return ($ret); } }