Skip to content

The Flysystem integration for the Yii framework.

License

Notifications You must be signed in to change notification settings

biller/yii2-flysystem

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flysystem (3.0) Extension for Yii 2

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);

About

The Flysystem integration for the Yii framework.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%