zoukankan      html  css  js  c++  java
  • 文件上传到七牛云

    安装laravel框架扩展

    composer  require zgldh/qiniu-laravel-storage

    config/app.php 中注册服务提供者:

    zgldhQiniuStorageQiniuFilesystemServiceProvider::class

    config/filesystems.php 里的 disks 中新增七牛配置:

      'qiniu' => [
            'driver'  => 'qiniu',
            'domains' => [
                'default'   => 'psz4iisb3.bkt.clouddn.com', //你的七牛域名
                'https'     => '',         //你的HTTPS域名
                'custom'    => '',     //你的自定义域名
            ],
            'access_key'=> 'O6qRwGm6EybA-ue0CZmqvkdkmCVkmj488r4aamSF',  //AccessKey
            'secret_key'=> 'SBr06jBZeBjpd4YeIq-Q_1YEdUmDepxfhLJqby0B',  //SecretKey
            'bucket'    => 'zhangjinchai',  //Bucket名字 存储空间
            'notify_url'=> '',  //持久化处理回调地址
        ],

    controller 代码

    <?php
    
    namespace AppHttpControllers;
    
    use IlluminateHttpRequest;
    
    use zgldhQiniuStorageQiniuStorage;
    
    class UploadController extends Controller
    
    {
    
        /**
    
         * 上传文件到七牛
    
         * @author 高伟
    
         * @date   2016-11-09T16:58:37+0800
    
         * @param  Request                  $request [description]
    
         * @return [type]                            [description]
    
         */
    
        public function uploadFile(Request $request)
    
        {
    
            // 判断是否有文件上传
    
            if ($request->hasFile('file')) {
    
                // 获取文件,file对应的是前端表单上传input的name
    
                $file = $request->file('file');
    
                // Laravel5.3中多了一个写法
    
                // $file = $request->file;
    
                // 初始化
    
                $disk = QiniuStorage::disk('qiniu');
    
                // 重命名文件
    
                $fileName = md5($file->getClientOriginalName().time().rand()).'.'.$file->getClientOriginalExtension();
    
                // 上传到七牛
    
                $bool = $disk->put('iwanli/image_'.$fileName,file_get_contents($file->getRealPath()));
    
                // 判断是否上传成功
    
                if ($bool) {
    
                    $path = $disk->downloadUrl('iwanli/image_'.$fileName);
    
                    return '上传成功,图片url:'.$path;
    
                }
    
                return '上传失败';
    
            }
    
            return '没有文件';
    
        }
    
    }

    上传成功

  • 相关阅读:
    700. Search in a Binary Search Tree
    100. Same Tree
    543. Diameter of Binary Tree
    257. Binary Tree Paths
    572. Subtree of Another Tree
    226. Invert Binary Tree
    104. Maximum Depth of Binary Tree
    1、解决sublime打开文档,出现中文乱码问题
    移植seetafaceengine-master、opencv到ARM板
    ubuntu16.04-交叉编译-SeetaFaceEngine-master
  • 原文地址:https://www.cnblogs.com/chaihtml/p/11203537.html
Copyright © 2011-2022 走看看