libratechie/think-filesystem

ThinkPHP flysystem 包

v1.0.0 2024-08-08 07:19 UTC

This package is auto-updated.

Last update: 2024-09-08 08:39:30 UTC


README

这是一个为ThinkPHP 8.0提供的文件系统扩展包,支持本地文件存储、七牛云OSS存储和阿里云OSS存储(阿里云)。此包无缝集成ThinkPHP,为跨多个存储后端管理文件存储提供了一个强大且灵活的解决方案。使用此包,开发人员可以轻松地在不同的存储选项之间切换,确保应用程序中文件处理的效率和可靠性。此包的将来版本将包括对其他存储系统的支持,进一步增强其多功能性和功能性。

要求

  • PHP >= 8.2
  • league/flysystem ^3.28

安装

$ composer require libratechie/think-filesystem

用法

方法1:使用直接类引用

您可以直接引用包提供的Filesystem类。这种方法允许您使用磁盘方法来指定您想操作的磁盘

use \Libratechie\Think\Filesystem;
$disk = Filesystem::disk('public');

方法2:使用ThinkPHP容器解析

或者,您可以利用ThinkPHP的容器来解决文件系统服务。这种方法在您想遵循依赖注入原则并使您的代码更具可测试性时很有用

$disk = app('filesystem')::disk('qiniu');

配置

本地

return [
    'disks'   => [
        //...
        'local'  => [
            'type' => 'local',
            'root' => app()->getRuntimePath() . 'storage',
        ],
        'public' => [
            'type'       => 'local',
            'root'       => app()->getRootPath() . 'public/storage',
            'url'        => '/storage',
            'visibility' => 'public',
        ],
        //...
    ],
];

阿里云

return [
    'disks'   => [
        //...
        'aliyun' => [
            'type'            => 'aliyun',
            'accessKeyId'     => '<aliyun access id>',
            'accessKeySecret' => '<aliyun access secret>',
            'bucket'          => '<bucket name>',
            'endpoint'        => '<endpoint address>',
            // 'domain'       => 'bucket.oss-cn-guangzhou.aliyuncs.com',
            // or with protocol: https://bucket.oss-cn-guangzhou.aliyuncs.com
        ]
        //...
    ],
];

域名配置:域名配置指定了您的阿里云OSS资源的对外访问路径。默认情况下,使用OSS默认域名访问HTML或图像资源可能会导致它们被下载为附件。要允许通过Web浏览器直接访问,您需要使用自定义域名。

七牛

return [
    'disks'   => [
        //...
        'qiniu'  => [
            'type'      => 'qiniu',
            'accessKey' => '<qiniu access key>',
            'secretKey' => '<qiniu secret key>',
            'bucket'    => '<bucket name>',
            'domain'    => 'xxxxx.hn-bkt.clouddn.com',
            // or with protocol: https://xxxxx.hn-bkt.clouddn.com
        ],
        //...
    ],
];

API

// Write files
$folderPath = '/path/to';
$file = request()->file('file');

// Use the default naming convention to write the file
$fileName = $disk->putFile($folderPath, $file);
// $fileName: /path/to/20240725/2697c763c84fe48d0166d0cd37181e19.jpg

// Use SHA-256 hash as the file name
$fileName = $disk->putFile($folderPath, $file, 'sha256');
// $fileName: /path/to/55/fd6b615cb02ce73c8e708ac62c9fe9c0cdd92d9161c57186e592d2b672e6e3.jpg

// Use a custom callback function to generate the file name
$fileName = $disk->putFile($folderPath, $file, function ($fileHash) {
    return 'custom' . DIRECTORY_SEPARATOR . md5($fileHash->getPathname());
});
// $fileName: /path/to/custom/db8fbf2c977c3fae6276521f788d5183.jpg
// Save the file with a specified file name
$fileName = $disk->putFileAs($folderPath, $file, 'custom.txt');
// Check whether a file exists.
$exists = $disk->fileExists('/path/to/custom.txt');
// Get file access path.
$exists = $disk->url('/path/to/custom.txt');
// Get file size.
$size = $disk->fileSize('/path/to/custom.txt');
// Get file mimeType.
$size = $disk->mimeType('/path/to/custom.txt');
// Copy files
$disk->copy('/path/to/file.txt', '/path/to/copy_file.txt');
// Move files
$disk->move('/path/to/custom.txt', '/path/to/moved_file.txt');
// Delete files
$disk->delete('/path/to/file.txt');