t-kanstantsin/yii2-flysystem

Yii 框架的 flysystem 扩展

安装: 308

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 77

类型:yii2-extension

1.0.0 2016-07-14 14:17 UTC

This package is not auto-updated.

Last update: 2024-09-18 20:59:36 UTC


README

Code Quality Packagist Version Total Downloads

此扩展为 Yii 框架提供了 Flysystem 集成。 Flysystem 是一个文件系统抽象,允许您轻松地将本地文件系统替换为远程文件系统。

安装

安装此扩展的首选方式是通过 composer

运行以下命令:

$ composer require creocoder/yii2-flysystem

或者添加以下内容到您的 composer.json 文件的 require 部分:

"creocoder/yii2-flysystem": "0.8.*"

配置

本地文件系统

按如下方式配置应用程序的 components

return [
    //...
    'components' => [
        //...
        'fs' => [
            'class' => 'creocoder\flysystem\LocalFilesystem',
            'path' => '@webroot/files',
        ],
    ],
];

FTP 文件系统

按如下方式配置应用程序的 components

return [
    //...
    'components' => [
        //...
        'ftpFs' => [
            'class' => 'creocoder\flysystem\FtpFilesystem',
            'host' => 'ftp.example.com',
            // 'port' => 21,
            // 'username' => 'your-username',
            // 'password' => 'your-password',
            // 'ssl' => true,
            // 'timeout' => 60,
            // 'root' => '/path/to/root',
            // 'permPrivate' => 0700,
            // 'permPublic' => 0744,
            // 'passive' => false,
            // 'transferMode' => FTP_TEXT,
        ],
    ],
];

NULL 文件系统

按如下方式配置应用程序的 components

return [
    //...
    'components' => [
        //...
        'nullFs' => [
            'class' => 'creocoder\flysystem\NullFilesystem',
        ],
    ],
];

AWS S3 文件系统

运行以下命令:

$ composer require league/flysystem-aws-s3-v3

或者添加以下内容到您的 composer.json 文件的 require 部分:

"league/flysystem-aws-s3-v3": "~1.0"

将以下内容添加到您的 composer.json 文件的 require 部分,并按如下方式配置应用程序的 components

return [
    //...
    'components' => [
        //...
        'awss3Fs' => [
            'class' => 'creocoder\flysystem\AwsS3Filesystem',
            'key' => 'your-key',
            'secret' => 'your-secret',
            'bucket' => 'your-bucket',
            'region' => 'your-region',
            // 'version' => 'latest',
            // 'baseUrl' => 'your-base-url',
            // 'prefix' => 'your-prefix',
            // 'options' => [],
            // 'endpoint' => 'http://my-custom-url'
        ],
    ],
];

Azure 文件系统

将以下内容添加到您的 composer.json 文件的 repositories 部分:

{
    "type": "pear",
    "url": "http://pear.php.net"
}

运行以下命令:

$ composer require league/flysystem-azure

或者添加以下内容到您的 composer.json 文件的 require 部分:

"league/flysystem-azure": "~1.0"

将以下内容添加到您的 composer.json 文件的 require 部分,并按如下方式配置应用程序的 components

return [
    //...
    'components' => [
        //...
        'azureFs' => [
            'class' => 'creocoder\flysystem\AzureFilesystem',
            'accountName' => 'your-account-name',
            'accountKey' => 'your-account-key',
            'container' => 'your-container',
        ],
    ],
];

复制文件系统

运行以下命令:

$ composer require league/flysystem-copy

或者添加以下内容到您的 composer.json 文件的 require 部分:

"league/flysystem-copy": "~1.0"

将以下内容添加到您的 composer.json 文件的 require 部分,并按如下方式配置应用程序的 components

return [
    //...
    'components' => [
        //...
        'copyFs' => [
            'class' => 'creocoder\flysystem\CopyFilesystem',
            'consumerKey' => 'your-consumer-key',
            'consumerSecret' => 'your-consumer-secret',
            'accessToken' => 'your-access-token',
            'tokenSecret' => 'your-token-secret',
            // 'prefix' => 'your-prefix',
        ],
    ],
];

Dropbox 文件系统

运行以下命令:

$ composer require league/flysystem-dropbox

或者添加以下内容到您的 composer.json 文件的 require 部分:

"league/flysystem-dropbox": "~1.0"

将以下内容添加到您的 composer.json 文件的 require 部分,并按如下方式配置应用程序的 components

return [
    //...
    'components' => [
        //...
        'dropboxFs' => [
            'class' => 'creocoder\flysystem\DropboxFilesystem',
            'token' => 'your-token',
            'app' => 'your-app',
            // 'prefix' => 'your-prefix',
        ],
    ],
];

Google Cloud 文件系统

运行以下命令:

$ composer require "superbalist/flysystem-google-storage": "^5.0"

并按如下方式配置应用程序的 components

return [
    //...
    'components' => [
        //...
        'googleCloudFs' => [
            'class' => 'creocoder\flysystem\GoogleCloudFilesystem',
            'projectId' => 'GOOGLE_PROJECT_ID',
            'bucket' => 'GOOGLE_BUCKET',
            'keyFilePath' => 'GOOGLE_KEY_FILE_PATH',
        ],
    ],
];

注意:凭据配置从 keyFile 读取。

GridFS 文件系统

运行以下命令:

$ composer require league/flysystem-gridfs

或者添加以下内容到您的 composer.json 文件的 require 部分:

"league/flysystem-gridfs": "~1.0"

将以下内容添加到您的 composer.json 文件的 require 部分,并按如下方式配置应用程序的 components

return [
    //...
    'components' => [
        //...
        'gridFs' => [
            'class' => 'creocoder\flysystem\GridFSFilesystem',
            'server' => 'mongodb://:27017',
            'database' => 'your-database',
        ],
    ],
];

Rackspace 文件系统

运行以下命令:

$ composer require league/flysystem-rackspace

或者添加以下内容到您的 composer.json 文件的 require 部分:

"league/flysystem-rackspace": "~1.0"

将以下内容添加到您的 composer.json 文件的 require 部分,并按如下方式配置应用程序的 components

return [
    //...
    'components' => [
        //...
        'rackspaceFs' => [
            'class' => 'creocoder\flysystem\RackspaceFilesystem',
            'endpoint' => 'your-endpoint',
            'region' => 'your-region',
            'username' => 'your-username',
            'apiKey' => 'your-api-key',
            'container' => 'your-container',
            // 'prefix' => 'your-prefix',
        ],
    ],
];

SFTP 文件系统

运行以下命令:

$ composer require league/flysystem-sftp

或者添加以下内容到您的 composer.json 文件的 require 部分:

"league/flysystem-sftp": "~1.0"

将以下内容添加到您的 composer.json 文件的 require 部分,并按如下方式配置应用程序的 components

return [
    //...
    'components' => [
        //...
        'sftpFs' => [
            'class' => 'creocoder\flysystem\SftpFilesystem',
            'host' => 'sftp.example.com',
            // 'port' => 22,
            'username' => 'your-username',
            'password' => 'your-password',
            'privateKey' => '/path/to/or/contents/of/privatekey',
            // 'timeout' => 60,
            // 'root' => '/path/to/root',
            // 'permPrivate' => 0700,
            // 'permPublic' => 0744,
        ],
    ],
];

WebDAV 文件系统

运行以下命令:

$ composer require league/flysystem-webdav

或者添加以下内容到您的 composer.json 文件的 require 部分:

"league/flysystem-webdav": "~1.0"

将以下内容添加到您的 composer.json 文件的 require 部分,并按如下方式配置应用程序的 components

return [
    //...
    'components' => [
        //...
        'webdavFs' => [
            'class' => 'creocoder\flysystem\WebDAVFilesystem',
            'baseUri' => 'your-base-uri',
            // 'userName' => 'your-user-name',
            // 'password' => 'your-password',
            // 'proxy' => 'your-proxy',
            // 'authType' => \Sabre\DAV\Client::AUTH_BASIC,
            // 'encoding' => \Sabre\DAV\Client::ENCODING_IDENTITY,
            // 'prefix' => 'your-prefix',
        ],
    ],
];

ZipArchive 文件系统

运行以下命令:

$ composer require league/flysystem-ziparchive

或者添加以下内容到您的 composer.json 文件的 require 部分:

"league/flysystem-ziparchive": "~1.0"

将以下内容添加到您的 composer.json 文件的 require 部分,并按如下方式配置应用程序的 components

return [
    //...
    'components' => [
        //...
        'ziparchiveFs' => [
            'class' => 'creocoder\flysystem\ZipArchiveFilesystem',
            'path' => '@webroot/files/archive.zip',
            // 'prefix' => 'your-prefix',
        ],
    ],
];

缓存功能

运行以下命令:

$ composer require league/flysystem-cached-adapter

或者添加以下内容到您的 composer.json 文件的 require 部分:

"league/flysystem-cached-adapter": "~1.0"

将以下内容添加到您的 composer.json 文件的 require 部分,并按如下方式配置 fsID 应用程序组件:

return [
    //...
    'components' => [
        //...
        'fsID' => [
            //...
            'cache' => 'cacheID',
            // 'cacheKey' => 'flysystem',
            // 'cacheDuration' => 3600,
        ],
    ],
];

复制功能

运行以下命令:

$ composer require league/flysystem-replicate-adapter

或者添加以下内容到您的 composer.json 文件的 require 部分:

"league/flysystem-replicate-adapter": "~1.0"

将以下内容添加到您的 composer.json 文件的 require 部分,并按如下方式配置 fsID 应用程序组件:

return [
    //...
    'components' => [
        //...
        'fsID' => [
            //...
            'replica' => 'anotherFsID',
        ],
    ],
];

全局可见性设置

按如下方式配置 fsID 应用程序组件:

return [
    //...
    'components' => [
        //...
        'fsID' => [
            //...
            'config' => [
                'visibility' => \League\Flysystem\AdapterInterface::VISIBILITY_PRIVATE,
            ],
        ],
    ],
];

插件使用

按如下方式配置任何应用程序组件:

return [
    //...
    'components' => [
        //...
        'fs' => [
            //... other config settings of compnent
            'pluginArray' => [
                // list of plugin classes, e.g.
            ],
        ],
    ],
];

使用方法

写入文件

写入文件

Yii::$app->fs->write('filename.ext', 'contents');

使用流内容写入文件

$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->writeStream('filename.ext', $stream);

更新文件

更新文件

Yii::$app->fs->update('filename.ext', 'contents');

使用流内容更新文件

$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->updateStream('filename.ext', $stream);

写入或更新文件

写入或更新文件

Yii::$app->fs->put('filename.ext', 'contents');

使用流内容写入或更新文件

$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->putStream('filename.ext', $stream);

读取文件

读取文件

$contents = Yii::$app->fs->read('filename.ext');

获取读取流

$stream = Yii::$app->fs->readStream('filename.ext');
$contents = stream_get_contents($stream);
fclose($stream);

检查文件是否存在

检查文件是否存在

$exists = Yii::$app->fs->has('filename.ext');

删除文件

删除文件

Yii::$app->fs->delete('filename.ext');

读取和删除文件

读取和删除文件

$contents = Yii::$app->fs->readAndDelete('filename.ext');

重命名文件

重命名文件

Yii::$app->fs->rename('filename.ext', 'newname.ext');

获取文件 MIME 类型

获取文件 MIME 类型

$mimetype = Yii::$app->fs->getMimetype('filename.ext');

获取文件时间戳

获取文件时间戳

$timestamp = Yii::$app->fs->getTimestamp('filename.ext');

获取文件大小

获取文件大小

$timestamp = Yii::$app->fs->getSize('filename.ext');

创建目录

创建目录

Yii::$app->fs->createDir('path/to/directory');

在写入更深路径时也会隐式创建目录

Yii::$app->fs->write('path/to/filename.ext');

删除目录

删除目录

Yii::$app->fs->deleteDir('path/to/filename.ext');

管理可见性

可见性是跨多个平台文件权限的抽象。可见性可以是公开的或私有的。

use League\Flysystem\AdapterInterface;

Yii::$app->fs->write('filename.ext', 'contents', [
    'visibility' => AdapterInterface::VISIBILITY_PRIVATE
]);

您还可以更改和检查现有文件的可见性

use League\Flysystem\AdapterInterface;

if (Yii::$app->fs->getVisibility('filename.ext') === AdapterInterface::VISIBILITY_PRIVATE) {
    Yii::$app->fs->setVisibility('filename.ext', AdapterInterface::VISIBILITY_PUBLIC);
}

列出内容

列出内容

$contents = Yii::$app->fs->listContents();

foreach ($contents as $object) {
    echo $object['basename']
        . ' is located at' . $object['path']
        . ' and is a ' . $object['type'];
}

默认情况下,Flysystem 以非递归方式列出顶级目录。您可以提供目录名称和递归布尔值以获取更精确的结果

$contents = Yii::$app->fs->listContents('path/to/directory', true);

列出路径

列出路径

$paths = Yii::$app->fs->listPaths();

foreach ($paths as $path) {
    echo $path;
}

列出具有确保存在特定元数据的路径

列出具有确保存在特定元数据的路径

$listing = Yii::$app->fs->listWith(
    ['mimetype', 'size', 'timestamp'],
    'optional/path/to/directory',
    true
);

foreach ($listing as $object) {
    echo $object['path'] . ' has mimetype: ' . $object['mimetype'];
}

使用显式元数据获取文件信息

使用显式元数据获取文件信息

$info = Yii::$app->fs->getWithMetadata('path/to/filename.ext', ['timestamp', 'mimetype']);
echo $info['mimetype'];
echo $info['timestamp'];

捐赠

通过 gratipay 支持此项目和 creocoder。

Support via Gratipay