一、安装
1.1 TP6.0时安装的,现在升级到TP6.1.2版本了,这个插件没有更新,换其它插件(不推荐,停用)
官网:thinkphp-filesystem-cloud: thinkphp6.0的第三方filesystem扩展包,支持上传阿里云、七牛云。
composer require thans/thinkphp-filesystem-cloud
1.2 tp6.1.2版本用yzh52521/think-filesystem这个
官网:yzh52521/think-filesystem · ThinkPHP
composer require yzh52521/think-filesystem
二: 在config/filesystem.php中添加配置(两个插件都要这个)
'local' => [
'type' => 'local',
'root' => app()->getRuntimePath() . 'upload',
],
'public' => [
// 磁盘类型
'type' => 'local',
// 磁盘路径
'root' => app()->getRootPath() . 'public/upload',
// 磁盘路径对应的外部URL路径
'url' => '/upload',
// 可见性 不能改
'visibility' => 'public',
],
'aliyun' => [
'type' => 'aliyun',
'accessId' => '******',
'accessSecret' => '******',
'bucket' => 'bucket',
'endpoint' => 'oss-cn-hongkong.aliyuncs.com',
'url' => 'http://oss-cn-hongkong.aliyuncs.com',
],
'qiniu' => [
'type' => 'qiniu',
'accessKey' => '******',
'secretKey' => '******',
'bucket' => 'bucket',
'domain' => 'https://youcdn.domain.com',
],
'qcloud' => [
'type' => 'qcloud',
'region' => '***', //bucket 所属区域 英文
'app_id' => '***', // 域名中数字部分
'secret_id' => '***',
'secret_key' => '***',
'bucket' => '***',
'timeout' => 60,
'connect_timeout' => 60,
'cdn' => '您的 CDN 域名',
'scheme' => 'https',
'read_from_cdn' => false,
]
'obs'=>[
'type' =>'obs',
'root' => '',
'key' => env('OBS_KEY'),
'secret' => env('OBS_SECRET'),
'bucket' => env('OBS_BUCKET'),
'endpoint' => env('OBS_ENDPOINT'),
'is_cname' => env('OBS_IS_CNAME', false), //true or false...
'security_token' => env('OBS_SECURITY_TOKEN'),//true or false...
],
's3'=>[
'type' =>'s3',
'credentials' => [
'key' => 'S3_KEY',
'secret' => 'S3_SECRET',
],
'region' => 'S3_REGION',
'version' => 'latest',
'bucket_endpoint' => false,
'use_path_style_endpoint' => false,
'endpoint' => 'S3_ENDPOINT',
'bucket_name' => 'S3_BUCKET',
],
'google'=>[
'type' =>'google',
'projectId' => 'GOOGLE_PROJECT_ID',//your-project-id
'bucket' => 'GOOGLE_BUCKET', //your-bucket-name
'prefix' => '', //optional-prefix
],
'sftp'=>[
'type' =>'sftp',
'host' => 'example.com',
// 基于基础的身份验证设置...
'username' => 'username',
'password' => 'password',
// 使用加密密码进行基于 SSH 密钥的身份验证的设置...
'privateKey' => null,
'passphrase' => null,
// 可选的 SFTP 设置
'port' => 22,
'root' => '/path/to/root',
'url' => '/path/to/root',
'timeout' => 10
]
上面是修正正确代码,官方代码中是有错误的
'accessKey' => '******',
'secretKey' => '******',
三: yzh52521/think-filesystem插件使用方法
官方demo
$file = $this->request->file( 'image' );
try {
validate(
['image' => [
// 限制文件大小(单位b),这里限制为4M
'fileSize' => 10 * 1024 * 1000,
// 限制文件后缀,多个后缀以英文逗号分割
'fileExt' => 'gif,jpg,png,jpeg'
]
])->check( ['image' => $file] );
$path = \yzh52521\filesystem\facade\Filesystem::disk( 'public' )->putFile( 'test',$file);
$url = \yzh52521\filesystem\facade\Filesystem::disk( 'public' )->url( $path );
return json( ['path' => $path,'url' => $url] );
} catch ( \think\exception\ValidateException $e ) {
echo $e->getMessage();
}
$path = \yzh52521\filesystem\facade\Filesystem::disk( 'public' )->putFile( 'test',$file);
$url = \yzh52521\filesystem\facade\Filesystem::disk( 'public' )->url( $path );
按官方上面的代码报错:
正确方法:
$path =(new \yzh52521\filesystem\Filesystem(app()))->disk($public_path)->putFile($path,$files);
$saveName = (new \yzh52521\filesystem\Filesystem(app()))->disk($public_path)->url($path);
或
use yzh52521\filesystem\Filesystem ;
$file = new Filesystem(app());
$path = $file->disk($public_path)->putFile($path,$files);
$saveName = $file->disk($public_path)->url($path);
如果还不行,再安装官网这个,
composer require topthink/think-filesystem
测试成功环境是两个都安装
四: thans插件使用方法
使用(Filesystem::disk('public')修改为Filesystem::disk('aliyun'))
请参考thinkphp文档 文档地址:https://www.kancloud.cn/manual/thinkphp6_0/1037639
//TP6 上传本地服务器
$savename = \think\facade\Filesystem::disk('public')->putFile( 'topic', $file);
//扩展包引用方法
$savename = \think\facade\Filesystem::disk('aliyun')->putFile( 'topic', $file);
$savename = \think\facade\Filesystem::disk('aliyun')->putFile( 'topic', $file); 就是这个名称
只要参数填写正确,只需要修改一个地方就可以直接使用第三方的OSS,非常方便.