salevan/file-storage

File storage abstraction layer for Yii2

This package's canonical repository appears to be gone and the package has been frozen as a result.

dev-master / 1.0.x-dev 2017-02-20 11:30 UTC

This package is auto-updated.

Last update: 2024-03-14 18:28:48 UTC


README

This extension provides file storage abstraction layer for Yii2.

For license information check the LICENSE-file.

Latest Stable Version Total Downloads Build Status

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist salevan/file-storage

or add

"salevan/file-storage": "*"

to the require section of your composer.json.

If you wish to use Amazon S3 storage, you should also install aws/aws-sdk-php version 2. Either run

php composer.phar require --prefer-dist aws/aws-sdk-php:~2.0

or add

"aws/aws-sdk-php": "~2.0"

to the require section of your composer.json.

Usage

This extension provides file storage abstraction layer for Yii2. This abstraction introduces 2 main terms: 'storage' and 'bucket'. 'Storage' - is a unit, which is able to store files using some particular approach. 'Bucket' - is a logical part of the storage, which has own specific attributes and serves some logical mean. Each time you need to read/write a file you should do it via particular bucket, which is always belongs to the file storage.

Example application configuration:

return [
    'components' => [
        'fileStorage' => [
            'class' => 'yii2tech\filestorage\local\Storage',
            'basePath' => '@webroot/files',
            'baseUrl' => '@web/files',
            'filePermission' => 0777,
            'buckets' => [
                'tempFiles' => [
                    'baseSubPath' => 'temp',
                    'fileSubDirTemplate' => '{^name}/{^^name}',
                ],
                'imageFiles' => [
                    'baseSubPath' => 'image',
                    'fileSubDirTemplate' => '{ext}/{^name}/{^^name}',
                ],
            ]
        ],
        // ...
    ],
    // ...
];

Example usage:

$bucket = Yii::$app->fileStorage->getBucket('tempFiles');

$bucket->saveFileContent('foo.txt', 'Foo content'); // create file with content
$bucket->deleteFile('foo.txt'); // deletes file from bucket
$bucket->copyFileIn('/path/to/source/file.txt', 'file.txt'); // copy file into the bucket
$bucket->copyFileOut('file.txt', '/path/to/destination/file.txt'); // copy file from the bucket
var_dump($bucket->fileExists('file.txt')); // outputs `true`
echo $bucket->getFileUrl('file.txt'); // outputs: 'http://domain.com/files/f/i/file.txt'

Following file storages are available with this extension:

  • [[\yii2tech\filestorage\local\Storage]] - stores files on the OS local file system.
  • [[\yii2tech\filestorage\amazon\Storage]] - stores files using Amazon simple storage service (S3).
  • [[\yii2tech\filestorage\hub\Storage]] - allows combination of different file storages.

Please refer to the particualr storage class for more details.