thinkphp简单三步整合系列-阿里云oss文件上传
2021/8/19 9:06:34
本文主要是介绍thinkphp简单三步整合系列-阿里云oss文件上传,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
thinkphp6 阿里云oss
基于 xxtime/flysystem-aliyun-oss 轻度封装tp
安装
composer require death_satan/thinkphp-aliyun-oss
初始化
修改配置 config/filesystem.php 文件
<?php return [ // 默认磁盘 'default' => env('filesystem.driver', 'local'), // 磁盘列表 'disks' => [ 'local' => [ 'type' => 'local', 'root' => app()->getRuntimePath() . 'storage', ], 'public' => [ // 磁盘类型 'type' => 'local', // 磁盘路径 'root' => app()->getRootPath() . 'public/storage', // 磁盘路径对应的外部URL路径 'url' => '/storage', // 可见性 'visibility' => 'public', ], //新增一个阿里云磁盘 'aliyun'=>[ 'type'=>'Aliyun',//驱动使用阿里云 'accessId' => '<aliyun access id>', 'accessSecret' => '<aliyun access secret>', 'bucket' => '<bucket name>', 'endpoint' => '<endpoint address>', // 'timeout' => 3600, // 'connectTimeout' => 10, // 'isCName' => false, // 'token' => '', ] // 更多的磁盘配置信息 ], ];
使用方法
通过filesystem使用
//通过门面使用 think\facade\Filesystem::disk('aliyun') //在控制器中通过注入使用 class TestControl{ public function Test(\think\Filesystem $filesystem) { $aliyun = $filesystem->disk('aliyun'); } }
文件上传
<?php namespace app\controller; use app\BaseController; use app\Request; use think\facade\Filesystem; class Index extends BaseController { public function index(Request $request) { //获取上传文件 $file = $request->file('image'); //通过filesystem进行上传 $url = Filesystem::disk('aliyun')->putFile('images', $file); if (!$url) new \exception('上传失败'); dd('上传成功,文件位置:' . $url); } }
这篇关于thinkphp简单三步整合系列-阿里云oss文件上传的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-29env: php: no such file or directory
- 2024-03-01php foreach break
- 2024-02-26Exception参数 php-icode9专业技术文章分享
- 2023-12-30PHP文件批量上传-icode9专业技术文章分享
- 2023-12-30thinkphp6 withJoin-icode9专业技术文章分享
- 2023-12-27MagicArray:像php一样,让Go业务代码不再卷!
- 2023-11-18centos7编译安装PHP教程。
- 2023-11-18centos7编译安装phpMyAdmin教程。
- 2023-10-13唱衰这么多年,PHP 仍然还是你大爷!
- 2023-07-25PHP8,性能更好,语法更好,类型安全更完善