creocoder/yii2-flysystem
Composer 安装命令:
composer require creocoder/yii2-flysystem
包简介
The flysystem extension for the Yii framework
README 文档
README
This extension provides Flysystem integration for the Yii framework. Flysystem is a filesystem abstraction which allows you to easily swap out a local filesystem for a remote one.
This version requires Flysystem 3.x and PHP 8.1+
Installation
The preferred way to install this extension is through composer.
Either run
$ composer require creocoder/yii2-flysystem
or add
"creocoder/yii2-flysystem": "^2.0"
to the require section of your composer.json file.
Configuring
Local filesystem
Configure application components as follows
return [ //... 'components' => [ //... 'fs' => [ 'class' => \creocoder\flysystem\LocalFilesystem::class, 'path' => '@webroot/files', ], ], ];
FTP filesystem
Either run
$ composer require league/flysystem-ftp
or add
"league/flysystem-ftp": "^3.0"
to the require section of your composer.json file and configure application components as follows
return [ //... 'components' => [ //... 'ftpFs' => [ 'class' => \creocoder\flysystem\FtpFilesystem::class, 'host' => 'ftp.example.com', // 'port' => 21, // 'username' => 'your-username', // 'password' => 'your-password', // 'ssl' => true, // 'timeout' => 90, // 'root' => '/path/to/root', // 'passive' => true, ], ], ];
In-Memory filesystem
Configure application components as follows
return [ //... 'components' => [ //... 'memoryFs' => [ 'class' => \creocoder\flysystem\InMemoryFilesystem::class, ], ], ];
AWS S3 filesystem
Either run
$ composer require league/flysystem-aws-s3-v3
or add
"league/flysystem-aws-s3-v3": "^3.0"
to the require section of your composer.json file and configure application components as follows
return [ //... 'components' => [ //... 'awss3Fs' => [ 'class' => \creocoder\flysystem\AwsS3Filesystem::class, 'key' => 'your-key', 'secret' => 'your-secret', 'bucket' => 'your-bucket', 'region' => 'your-region', // 'version' => 'latest', // 'prefix' => 'your-prefix', // 'endpoint' => 'http://my-custom-url' ], ], ];
Azure Blob Storage filesystem
Either run
$ composer require league/flysystem-azure-blob-storage
or add
"league/flysystem-azure-blob-storage": "^3.0"
to the require section of your composer.json file and configure application components as follows
return [ //... 'components' => [ //... 'azureFs' => [ 'class' => \creocoder\flysystem\AzureFilesystem::class, 'accountName' => 'your-account-name', 'accountKey' => 'your-account-key', 'container' => 'your-container', // 'prefix' => 'your-prefix', ], ], ];
Dropbox filesystem
Either run
$ composer require spatie/flysystem-dropbox
or add
"spatie/flysystem-dropbox": "^3.0"
to the require section of your composer.json file and configure application components as follows
return [ //... 'components' => [ //... 'dropboxFs' => [ 'class' => \creocoder\flysystem\DropboxFilesystem::class, 'token' => 'your-token', // 'prefix' => 'your-prefix', ], ], ];
Google Cloud Storage filesystem
Run
$ composer require league/flysystem-google-cloud-storage
and configure application components as follows
return [ //... 'components' => [ //... 'googleCloudFs' => [ 'class' => \creocoder\flysystem\GoogleCloudFilesystem::class, 'projectId' => 'GOOGLE_PROJECT_ID', 'bucket' => 'GOOGLE_BUCKET', 'keyFilePath' => '@app/config/google-credentials.json', // 'prefix' => 'your-prefix', ], ], ];
Note: Credential configuration is read from the keyFilePath.
GridFS filesystem
Either run
$ composer require league/flysystem-gridfs
or add
"league/flysystem-gridfs": "^3.0"
to the require section of your composer.json file and configure application components as follows
return [ //... 'components' => [ //... 'gridFs' => [ 'class' => \creocoder\flysystem\GridFSFilesystem::class, 'uri' => 'mongodb://localhost:27017', 'database' => 'your-database', // 'bucketName' => 'fs', ], ], ];
SFTP filesystem
Either run
$ composer require league/flysystem-sftp-v3
or add
"league/flysystem-sftp-v3": "^3.0"
to the require section of your composer.json file and configure application components as follows
return [ //... 'components' => [ //... 'sftpFs' => [ 'class' => \creocoder\flysystem\SftpFilesystem::class, 'host' => 'sftp.example.com', // 'port' => 22, 'username' => 'your-username', 'password' => 'your-password', // 'privateKey' => '/path/to/or/contents/of/privatekey', // 'passphrase' => 'your-passphrase', // 'timeout' => 10, // 'root' => '/path/to/root', ], ], ];
WebDAV filesystem
Either run
$ composer require league/flysystem-webdav
or add
"league/flysystem-webdav": "^3.0"
to the require section of your composer.json file and configure application components as follows
return [ //... 'components' => [ //... 'webdavFs' => [ 'class' => \creocoder\flysystem\WebDAVFilesystem::class, 'baseUri' => 'your-base-uri', // 'userName' => 'your-user-name', // 'password' => 'your-password', // 'proxy' => 'your-proxy', // 'prefix' => 'your-prefix', ], ], ];
ZipArchive filesystem
Either run
$ composer require league/flysystem-ziparchive
or add
"league/flysystem-ziparchive": "^3.0"
to the require section of your composer.json file and configure application components as follows
return [ //... 'components' => [ //... 'ziparchiveFs' => [ 'class' => \creocoder\flysystem\ZipArchiveFilesystem::class, 'path' => '@webroot/files/archive.zip', // 'prefix' => 'your-prefix', ], ], ];
Global visibility settings
Configure fsID application component as follows
use League\Flysystem\Visibility; return [ //... 'components' => [ //... 'fsID' => [ //... 'config' => [ 'visibility' => Visibility::PRIVATE, ], ], ], ];
Usage
Writing files
To write a file
Yii::$app->fs->write('filename.ext', 'contents');
To write a file using stream contents
$stream = fopen('/path/to/somefile.ext', 'r+'); Yii::$app->fs->writeStream('filename.ext', $stream);
Reading files
To read a file
$contents = Yii::$app->fs->read('filename.ext');
To retrieve a read-stream
$stream = Yii::$app->fs->readStream('filename.ext'); $contents = stream_get_contents($stream); fclose($stream);
Checking if a file exists
To check if a file exists
$exists = Yii::$app->fs->fileExists('filename.ext');
To check if a directory exists
$exists = Yii::$app->fs->directoryExists('path/to/directory');
Deleting files
To delete a file
Yii::$app->fs->delete('filename.ext');
Moving files
To move a file
Yii::$app->fs->move('filename.ext', 'newname.ext');
Copying files
To copy a file
Yii::$app->fs->copy('filename.ext', 'copy.ext');
Getting files mimetype
To get a file mimetype
$mimetype = Yii::$app->fs->mimeType('filename.ext');
Getting files last modified timestamp
To get a file last modified timestamp
$timestamp = Yii::$app->fs->lastModified('filename.ext');
Getting files size
To get a file size
$size = Yii::$app->fs->fileSize('filename.ext');
Creating directories
To create a directory
Yii::$app->fs->createDirectory('path/to/directory');
Directories are also made implicitly when writing to a deeper path
Yii::$app->fs->write('path/to/filename.ext', 'contents');
Deleting directories
To delete a directory
Yii::$app->fs->deleteDirectory('path/to/directory');
Managing visibility
Visibility is the abstraction of file permissions across multiple platforms. Visibility can be either public or private.
use League\Flysystem\Visibility; Yii::$app->fs->write('filename.ext', 'contents', [ 'visibility' => Visibility::PRIVATE ]);
You can also change and check visibility of existing files
use League\Flysystem\Visibility; if (Yii::$app->fs->visibility('filename.ext') === Visibility::PRIVATE) { Yii::$app->fs->setVisibility('filename.ext', Visibility::PUBLIC); }
Listing contents
To list contents
$contents = Yii::$app->fs->listContents('', false); foreach ($contents as $item) { echo $item->path(); echo $item->isFile() ? 'file' : 'directory'; }
By default Flysystem lists the top directory non-recursively. You can supply a directory name and recursive boolean to get more precise results
$contents = Yii::$app->fs->listContents('path/to/directory', true);
Upgrading from v1
If you are upgrading from the Flysystem v1 version of this extension, please note the following breaking changes:
Removed Features
- Caching: The cached adapter is no longer available in Flysystem v3
- Replication: The replicate adapter is no longer available in Flysystem v3
- Rackspace: The Rackspace adapter has been removed as the service is discontinued
- NullFilesystem: Replaced with
InMemoryFilesystem
API Changes
| v1 Method | v3 Method |
|---|---|
has($path) |
fileExists($path) / directoryExists($path) |
createDir($path) |
createDirectory($path) |
deleteDir($path) |
deleteDirectory($path) |
rename($path, $newpath) |
move($source, $destination) |
getTimestamp($path) |
lastModified($path) |
getMimetype($path) |
mimeType($path) |
getSize($path) |
fileSize($path) |
getVisibility($path) |
visibility($path) |
update() / put() |
write() |
updateStream() / putStream() |
writeStream() |
Visibility Constants
// Old (v1) use League\Flysystem\AdapterInterface; AdapterInterface::VISIBILITY_PRIVATE; AdapterInterface::VISIBILITY_PUBLIC; // New (v3) use League\Flysystem\Visibility; Visibility::PRIVATE; Visibility::PUBLIC;
Donating
Support this project and others by creocoder via gratipay.
creocoder/yii2-flysystem 适用场景与选型建议
creocoder/yii2-flysystem 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.71M 次下载、GitHub Stars 达 286, 最近一次更新时间为 2015 年 01 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「filesystem」 「s3」 「WebDAV」 「aws」 「azure」 「files」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 creocoder/yii2-flysystem 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 creocoder/yii2-flysystem 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 creocoder/yii2-flysystem 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A SDK for working with B2 cloud storage.
A PHP class providing static methods for reading, writing, copying, moving, and deleting files and directories, MIME type detection, image size detection, and file permission management
Webdav Adapter for Flysystem
This package includes a script and fail2ban configuration that allows you to use fail2ban when utilizing AWS elastic load balancer (ELB) and an apache webserver.
The flysystem adapter for yandex disk rest api.
Elastic Driver for Laravel Scout
统计信息
- 总下载量: 1.71M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 293
- 点击次数: 29
- 依赖项目数: 77
- 推荐数: 2
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-01-17