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.
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.
Configure application components
as follows
return [
//...
'components' => [
//...
'fs' => [
'class' => 'biller\flysystem\LocalFilesystem',
'path' => '@webroot/files',
],
],
];
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'
],
],
];
Configure fsID
application component as follows
return [
//...
'components' => [
//...
'fsID' => [
//...
'config' => [
'visibility' => \League\Flysystem\Visibility::PRIVATE,
],
],
],
];
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);
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);
To check if a file exists
$exists = Yii::$app->fs->fileExists('filename.ext');
To delete file
Yii::$app->fs->delete('filename.ext');
To get file mimetype
$mimetype = Yii::$app->fs->mimeType('filename.ext');
To get file timestamp
$timestamp = Yii::$app->fs->lastModified('filename.ext');
To get file size
$timestamp = Yii::$app->fs->fileSize('filename.ext');
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');
To delete directory
Yii::$app->fs->deleteDirectory('path/to/filename.ext');
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);
}
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);