selden1992 / think-flysystem
thinkphp 集成的文件系统,提供本地存储和第三方存储,以一致的API进行操作
Requires
- php: >=5.6.4
- league/flysystem: ^1.0
This package is not auto-updated.
Last update: 2024-09-15 02:52:01 UTC
README
think PHP5集成Filesystem
think-filesystem 是基于 Frank de Jonge 开发的 PHP 包 Flysystem 提供的强大的文件系统抽象。think-filesystem 文件系统集成进行了汉化和简化,当然也进行了一些本地化封装。
安装
通过 composer 安装此扩展是首选方法。
运行以下命令之一:
$ composer require selden1992/think-flysystem
或者
"selden1992/think-flysystem": "dev-master"
将以下内容添加到您的 composer.json
文件的 require
部分:
配置
将 /vendor/selden1992/think-flysystem/config/flysystem.php 复制到 CONF_PATH.'extra/flysystem.php'
<?php /** * 演示配置 */ return [ // 默认驱动 'default'=>'local', // 本地驱动 'local'=>[ 'adapter_class'=>\Think\flysystem\adapter\Local::class, // 跟目录 'root'=>'./files/', // 权限参数 'permissions'=>[ 'file' => [ 'public' => 0744, 'private' => 0700, ], 'dir' => [ 'public' => 0755, 'private' => 0700, ] ], // 目录别名 'alias'=>[ 'image'=>'image/user/', ], ], // ftp 扩展 'ftp'=>[ 'adapter_class'=>\Think\flysystem\adapter\Ftp::class, // 权限参数 'permissions'=>[ 'host' => 'ftp.example.com', 'username' => 'username', 'password' => 'password', /** optional config settings */ 'port' => 21, 'root' => '/path/to/root', 'passive' => true, 'ssl' => true, 'timeout' => 30, ], // 目录别名 'alias'=>[ 'image'=>'image/user/', ], ], // sftp 扩展 'sftp'=>[ 'adapter_class'=>\Think\flysystem\adapter\Sftp::class, // 权限参数 'permissions'=>[ 'host' => 'example.com', 'port' => 21, 'username' => 'username', 'password' => 'password', 'privateKey' => 'path/to/or/contents/of/privatekey', 'root' => '/path/to/root', 'timeout' => 10, ], // 目录别名 'alias'=>[ 'image'=>'image/user/', ], ], ];
如果 ftp 提示无法写入,可以设置一个 777 权限的目录来调试是否权限受到影响
putenv(‘TMPDIR=/Users/cyz/web/test’);
普通使用
<?php namespace app\index\controller; use Think\flysystem\Files; class Index { public function index() { // 普通写入 Files::put('log/test.log',time()); // 目录别名写入 Files::alias('log_alias')->put('test2.log',time()); // 指定驱动 Files::disk('local')->put('test3.log',time()); // 普通读取 echo Files::read('log/test.log'); // 目录别名读取 echo Files::alias('log_alias')->read('test2.log'); // 复制 Files::copy('log/test.log','log/image1.log'); // 第二参数路径使用别名 Files::copy('log/test.log',['alias'=>'local','path'=>'image1.log']); // 获取别名真实路径 echo Files::getAliasPath('image'); /** * ftp 操作 */ Files::disk('ftp')->put('log/test4.log',time()); echo Files::disk('ftp')->alias('log_alias')->read('test4.log'); /** * sftp 操作 * 需要按 composer require league/flysystem-sftp */ Files::disk('sftp')->put('log/test4.log',time()); echo Files::disk('sftp')->alias('log_alias')->read('test4.log'); } }
alias 目录别名的作用是快速更换目录,例如开发阶段,log_alias 指向 root/log/ 下,部署阶段更改为 web/log/
其他使用方法和 flysystem 一致
API 常用用法
写文件
Files::write('path/to/file.txt', 'contents');
更新文件
Files::update('path/to/file.txt', 'new contents');
写或更新文件
Files::put('path/to/file.txt', 'contents');
读取文件
$contents = Files::read('path/to/file.txt');
检查文件是否存在
$exists = Files::has('path/to/file.txt');
注意:这仅对文件而不是目录具有一致的行为。在 Flysystem 中,目录不太重要,它们是隐式创建的,常常被忽略,因为不是每个适配器(文件系统类型)都支持目录。
删除文件
Files::delete('path/to/file.txt');
读取和删除
$contents = Files::readAndDelete('path/to/file.txt');
重命名文件
Files::rename('filename.txt', 'newname.txt');
复制文件
Files::copy('filename.txt', 'duplicate.txt');
获取Mimetypes
$mimetype = Files::getMimetype('path/to/file.txt');
获取时间戳
$timestamp = Files::getTimestamp('path/to/file.txt');
获取文件大小
$size = Files::getSize('path/to/file.txt');
创建目录
Files::createDir('path/to/nested/directory');
当写入更深的路径时,也隐含地指定了目录
Files::write('path/to/file.txt', 'contents');
删除目录
Files::deleteDir('path/to/directory');
上述方法将递归删除目录
注意:Flysystem API使用的所有路径都是相对于适配器根目录的。