Git Product home page Git Product logo

yii2-flysystem's Introduction

Flysystem (2.0) Extension for Yii 2

Notice: Only adapts AWS S3 and local filesystems.

Based on: https://github.com/creocoder/yii2-flysystem

This extension provides Flysystem 2.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:^2.0

or add

"biller/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' => 'biller\flysystem\LocalFilesystem',
            'path' => '@webroot/files',
        ],
    ],
];

AWS S3 filesystem

Either run

$ composer require league/flysystem-aws-s3-V3:^2.4

or add

"league/flysystem-aws-s3-V3": "^2.4"

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

yii2-flysystem's People

Contributors

creocoder avatar schmunk42 avatar feder98 avatar mikk150 avatar kesselb avatar pgaultier avatar huiyang avatar handcode avatar k-timoshenko avatar dizews avatar tonisormisson avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.