Home  >  Article  >  php教程  >  laravel上传文件到七牛云存储

laravel上传文件到七牛云存储

PHP中文网
PHP中文网Original
2016-05-22 18:27:121618browse


    public function store(Request $request)
    {
        $file = $request->file('photo') ;
        if(!$file->isValid()){
            return back() ;
        }
        // 需要填写你的 Access Key 和 Secret Key
        $accessKey = 'CiFDXNiHYC5wlJJfQTGzcfT8dqy01a-xyFLyr71b';
        $secretKey = 'Kbbk5jbF-G45QxWZza3H6zC_cl82vrMpa7f__QEQ';
        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);
        // 要上传的空间
        $bucket = 'laravel';
        // 生成上传 Token
        $token = $auth->uploadToken($bucket);
        // 要上传文件的本地路径
        $filePath = $file->getRealPath();
        // 上传到七牛后保存的文件名
        $key = time().rand(999,9999).".jpg";
        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        echo "\n====> putFile result: \n";
        if ($err !== null) {
            var_dump($err);
        } else {
            $photo = 'http://ddd.clouddn.com/'.$ret['key'] ;
            var_dump($ret);
        }
    }

                   

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn