Home >Backend Development >PHP Tutorial >An example of uploading images using Qiniu Cloud in Laravel

An example of uploading images using Qiniu Cloud in Laravel

黄舟
黄舟Original
2017-09-08 09:32:171987browse


Register Qiniu Cloud account

Open Qiniu Cloud registration account and perform real-name authentication. After successful authentication, you can view your private key
An example of uploading images using Qiniu Cloud in Laravel

Install jdk extension

Confirm that Composer is installed on this machine. If not, please install Composer on Baidu yourself. Enter composer require qiniu/php-sdk in the console and wait for the installation prompt to be successful

 // 引入鉴权类
    use Qiniu\Auth;    // 引入上传类
    use Qiniu\Storage\UploadManager;    // 需要填写你的 Access Key 和 Secret Key
    $accessKey = 'Access_Key';    $secretKey = 'Secret_Key';    // 构建鉴权对象
    $auth = new Auth($accessKey, $secretKey);    // 要上传的空间
    $bucket = 'Bucket_Name';    // 生成上传 Token
    $token = $auth->uploadToken($bucket);    // 要上传文件的本地路径
    $filePath = './php-logo.png';    // 上传到七牛后保存的文件名
    $key = 'my-php-logo.png';    // 初始化 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 {
        var_dump($ret);
    }

Complete

The above is the detailed content of An example of uploading images using Qiniu Cloud in Laravel. For more information, please follow other related articles on the PHP Chinese website!

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