1. 先去七牛雲官網下載sdkdom
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); } }