thinkphp5 tp5 七牛雲 上傳圖片


七牛sdk地址https://files.cnblogs.com/files/zonglonglong/qiniu-php-sdk-7.2.2.rar

首先下載php的sdk將文件夾放到vendor

然后在tp5的配置文件里加上

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

return [
    // +----------------------------------------------------------------------
    // | 七牛雲設置,zll這里是七牛雲的配置,七牛雲上傳用的就是這四個參數
    // +----------------------------------------------------------------------
    'ACCESSKEY' => '*******',//你的accessKey
    'SECRETKEY' => '*****',//你的secretKey
    'BUCKET' => '***',//上傳的空間
    'DOMAIN'=>'****.bkt.clouddn.com',//空間綁定的域名
    // +----------------------------------------------------------------------
    // | 應用設置
    // +----------------------------------------------------------------------

    // 應用命名空間
    'app_namespace'          => 'app',
    // 應用調試模式
    'app_debug'              => true,
    // 應用Trace
    'app_trace'              => false,

然后寫php文件,引入七牛的sdk

<?php
namespace app\index\controller;

use think\Db;
use think\Controller;
use think\Session;

use Qiniu\Auth as Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
class Base extends Controller
{
    public function _initialize(){
        
    }
   /**
     * 圖片上傳
     * @return String 圖片的完整URL
     */
    public function upload()
    {
        if(request()->isPost()){
            $file = request()->file('image');
            // 要上傳圖片的本地路徑
            $filePath = $file->getRealPath();
            $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后綴
            //獲取當前控制器名稱
            // $controllerName=$this->getContro();
            // 上傳到七牛后保存的文件名
            $key =substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
            require_once APP_PATH . '/../vendor/qiniu-php-sdk-7.2.2/autoload.php';
            // 需要填寫你的 Access Key 和 Secret Key
            $accessKey = config('ACCESSKEY');
            $secretKey = config('SECRETKEY');
            // 構建鑒權對象
            $auth = new Auth($accessKey, $secretKey);
            // 要上傳的空間
            $bucket = config('BUCKET');
            $domain = config('DOMAINImage');
            $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
                return json(["err"=>0,"msg"=>"上傳完成","data"=>($domain . $ret['key'])]);
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM