biller / yii2-flysystem
The flysystem 3.0 extension for the Yii framework
Installs: 193 700
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 79
Type:yii2-extension
Requires
- php: ^8.1
- league/flysystem: ^3.0
- league/flysystem-aws-s3-v3: ^3.27
- league/flysystem-memory: ^3.0
- yiisoft/yii2: ^2.0
README
Notice: Only adapts AWS S3, InMemory and local filesystems.
Based on: https://github.com/creocoder/yii2-flysystem
This extension provides Flysystem 3.0 integration for the Yii framework. Flysystem is a filesystem abstraction which allows you to easily swap out a local filesystem for a remote one.
Installation
The preferred way to install this extension is through composer.
Either run
$ composer require biller/yii2-flysystem:^3.0
or add
"biller/yii2-flysystem": "^3.0"
to the require
section of your composer.json
file.
Configuring
Local filesystem
Configure application components
as follows
return [ //... 'components' => [ //... 'fs' => [ 'class' => 'biller\flysystem\LocalFilesystem', 'path' => '@webroot/files', ], ], ];
AWS S3 filesystem
Either run
$ composer require league/flysystem-aws-s3-V3:^3.15
or add
"league/flysystem-aws-s3-V3": "^3.15"
to the require
section of your composer.json
file and configure application components
as follows
return [ //... 'components' => [ //... 'awss3Fs' => [ 'class' => 'biller\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' ], ], ];
Global visibility settings
Configure fsID
application component as follows
return [ //... 'components' => [ //... 'fsID' => [ //... 'config' => [ 'visibility' => \League\Flysystem\Visibility::PRIVATE, ], ], ], ];
Usage
Writing or updating files
To write or update file
Yii::$app->fs->write('filename.ext', 'contents');
To write or update file using stream contents
$stream = fopen('/path/to/somefile.ext', 'r+'); Yii::$app->fs->writeStream('filename.ext', $stream);
Reading files
To read 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');
Deleting files
To delete file
Yii::$app->fs->delete('filename.ext');
Getting files mimetype
To get file mimetype
$mimetype = Yii::$app->fs->mimeType('filename.ext');
Getting files timestamp
To get file timestamp
$timestamp = Yii::$app->fs->lastModified('filename.ext');
Getting files size
To get file size
$timestamp = Yii::$app->fs->fileSize('filename.ext');
Creating directories
To create 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');
Deleting directories
To delete directory
Yii::$app->fs->deleteDirectory('path/to/filename.ext');
Managing visibility
Visibility is the abstraction of file permissions across multiple platforms. Visibility can be either public or private.
use League\Flysystem\AdapterInterface; Yii::$app->fs->write('filename.ext', 'contents', [ 'visibility' => \League\Flysystem\Visibility.PRIVATE ]);
You can also change and check visibility of existing files
use League\Flysystem\AdapterInterface; if (Yii::$app->fs->visibility('filename.ext') === \League\Flysystem\Visibility::PRIVATE) { Yii::$app->fs->setVisibility('filename.ext', \League\Flysystem\Visibility.PUBLIC); }
Listing contents
To list contents
$contents = Yii::$app->fs->listContents(); foreach ($contents as $object) { echo $object['basename'] . ' is located at' . $object['path'] . ' and is a ' . $object['type']; }
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);