您好,欢迎来到互利cms 官方网站-内容管理系统,让你体验不一样的建站方式,更简单,更安全,更灵活
  • 登陆-注册
  • 使用手册
  • 常见问题
  • 全国服务热线:18173153117

    think5.1x使用心得教程->thinkphp 5.1下载七牛云扩展
  • thinkphp 5.1下载七牛云扩展
  • 浏览次数:101 作者:旭日东升 日期:2019-06-18 11:42:18
  • ThinkPHP5.0安装七牛云扩展


    composer安装七牛SDK

    composer require qiniu/php-sdk
    

    config文件保存七牛云配置信息

    //七牛云空间配置
        'qiniu' => [
            'AccessKey'     =>      'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
            'SecretKey'     =>      'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy',
            'bucket'        =>      'zzzzz',
            'domain'        =>      'demo.domain.net/'
        ]
    

    创建Qiniu Model,实现上传功能

    
    <?php
    /**
     * Created by PhpStorm.
     * User: zgcli
     * Date: 2019/2/12
     * Time: 16:32
     */
    
    namespace app\admin\model;
    
    use think\Model;
    use Qiniu\Auth;
    use Qiniu\Storage\UploadManager;
    
    class Qiniu extends Model
    {
        private $AccessKey;
        private $SecretKey;
        private $bucket;
        private $auth;
    
        function __construct()
        {
            parent::__construct();
            $this->AccessKey = config('qiniu.AccessKey');
            $this->SecretKey = config('qiniu.SecretKey');
            $this->bucket = config('qiniu.bucket');
            vendor('qiniu.php-sdk.autoload');
            $this->auth = new Auth($this->AccessKey, $this->SecretKey);
        }
    
        /**
         * @description 七牛上传文件
         * @param string $fileName 上传文件的name值
         * @param string $bucket 上传至七牛的指定空间
         * @return array 上传结果信息
         */
        public function upload($fileName = '', $bucket = '')
        {
    
            //文件获取、处理
            $file = request()->file($fileName);
    
            // 上传文件的本地路径
            $filePath = $file->getRealPath();
    
            //文件后缀
            $extension = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
    
            //获取七牛token
            $bucket = empty($bucket) ? $this->bucket : $bucket;
            $token = $this->auth->uploadToken($bucket);
    
            //上传到七牛后保存的文件名
            $key = time() . rand(0, 9999) . '.' . $extension;
    
            //初始化UploadManager对象
            $uploadManager = new UploadManager();
    
            //文件上传
            list($result, $error) = $uploadManager->putFile($token, $key, $filePath);
            if ($error !== null) {
                return ['errNo' => 1, 'errMsg' => $error, 'data' => $this->SecretKey];
            } else {
                return ['errNo' => 0, 'data' => ['bucket' => $this->bucket, 'key' => $key, 'url' => config('qiniu.domain').$key]];
            }
        }
    
        /**
         * 获取私有空间或使用了原图保护功能的图片文件地址
         * @param string $url 格式:http://domain/key[文件名]?e=时间戳
         * @return string 可访问的url地址:http://domain/key[文件名]?e=时间戳&token='token'
         */
        public function getSignedUrl($url)
        {
            $signedUrl = $this->auth->privateDownloadUrl($url);
            //该url地址需要验证是否可访问。
            return $signedUrl;
        }
    }
    
    

    测试上传功能

    Controller:

        public function test()
        {
            if ($this->request->isPost()) {
                $qiniu = new \app\admin\model\Qiniu();
                $data = $qiniu->upload('imgFile');
                var_dump($data);
            }
            return $this->view->fetch();
        }
    

    View:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>测试七牛云上传</title>
    </head>
    <body>
    <form action="" enctype="multipart/form-data" method="post">
        <input type="file" name="imgFile">
        <input type="submit">
    </form>
    </body>
    </html>
  • 站内地图

    Copyright 2018-2028 hlcms. All Rights Reserved. 湘ICP备13008873号-1