2017 © Pedro Peláez
 

yii2-extension yii2-upload-manager

Simplifies file storing in Yii 2.

image

herroffizier/yii2-upload-manager

Simplifies file storing in Yii 2.

  • Thursday, March 31, 2016
  • by herroffizier
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1,265 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 4 % Grown

The README.md

Yii2 Upload Manager

Build Status Scrutinizer Code Quality Code Coverage, (*1)

Yii2 Upload Manager is a small extension that organizes file uploads and takes control over upload paths and urls., (*2)

Features

  • Groups uploads into folders and folder structures with any depth.
  • Divides upload folders into subfolders to avoid storing many files in one folder.
  • Fights against file name collisions.
  • Uses transparent file name generation mechanism.
  • 100% code coverage :-)

Installation

Install extension with Composer:, (*3)

composer require herroffizier/yii2-upload-manager:@stable

Add extension to your application config:, (*4)

'components' => [

    // ...

    'uploads' => [
        'class' => 'herroffizier\yii2um\UploadManager',
        // path to upload folder
        'uploadDir' => '@webroot/upload',
        // url to upload filder
        'uploadUrl' => '@web/upload',
    ],

    // ...
]

There is no need to create upload folder manually. Extension will make it automatically., (*5)

Usage

Storing files.

Extension provides few ways to store files., (*6)

Simply copy file to upload folder:, (*7)

$filePath = 
    Yii::$app->uploads->saveFile(
        // upload group
        'useless-files',
        // upload file name
        'file.txt',
        // original file name
        '/tmp/somefile.txt'
    );

Move file to upload folder:, (*8)

$filePath = 
    Yii::$app->uploads->moveFile(
        // upload group
        'useless-files',
        // upload file name
        'file.txt',
        // original file name
        '/tmp/somefile.txt'
    );

Save raw data as file in upload folder:, (*9)

$content = 'test';

$filePath = 
    Yii::$app->uploads->saveContent(
        // upload group
        'useless-files',
        // upload file name
        'file.txt',
        // file content
        $content
    );

Save \yii\web\UploadedFile instance to uplaod foder:, (*10)

$upload = \yii\web\UploadedFile::getInstance(/* ... */);

$filePath = 
    Yii::$app->uploads->saveUpload(
        // upload group
        'useless-files',
        // \yii\web\UploadedFile instance
        $upload
    );

As you may notice, all methods described above return $filePath value which is relative path to uploaded file and may be considered as unique upload id., (*11)

It can be converted to absolute file by method getAbsolutePath or to absolute url by method getUrl:, (*12)

// get absolute path
$absoluteFilePath = Yii::$app->uploads->getAbsolutePath($filePath);

// get url
$relativeUrl = Yii::$app->uploads->getUrl($filePath);

Name collisions

By deafult if you'll try to save file that already exists extension will throw an exception. Such behavior is not always suitable and you definitely don't want to solve each collision manually. So you have two different strategies which solve collisions automatically., (*13)

First strategy is to overwrite existing file silently. Such approach may be suitable for saving user avatars, for example., (*14)

Second strategy is to add incremental index to file name in case of collision. This strategy may be applied when dealing with user file uploads and original file names are important., (*15)

Now let's find out how to apply these strategies., (*16)

Strategy (throw exception, overwrite, add index) is identified by constant defined in \herroffizier\yii2um\UploadManager class: \herroffizier\yii2um\UploadManager::STRATEGY_KEEP, \herroffizier\yii2um\UploadManager::STRATEGY_OVERWRITE and \herroffizier\yii2um\UploadManager::STRATEGY_RENAME., (*17)

Also both methods saveContent and saveUpload have optional last parameter named $overwriteStrategy to which one of constants may be passed. Default value for $overwriteStrategy is \herroffizier\yii2um\UploadManager::STRATEGY_KEEP., (*18)

To sum up, let's take a look at example. Following code will throw an exception:, (*19)

Yii::$app->uploads->saveContent(
    'useless-files', 
    'file.txt', 
    'test 1'
);

Yii::$app->uploads->saveContent(
    'useless-files', 
    'file.txt', 
    'test 2'
);

Hovewer, this code will work correctly because we applied overwrite strategy:, (*20)

Yii::$app->uploads->saveContent(
    'useless-files', 
    'file.txt', 
    'test 1'
);

Yii::$app->uploads->saveContent(
    'useless-files', 
    'file.txt', 
    'test 2', 
    \herroffizier\yii2um\UploadManager::STRATEGY_OVERWRITE
);

Now file.txt contains test 2 string., (*21)

Finally, let's apply rename strategy:, (*22)

$filePath1 = Yii::$app->uploads->saveContent(
    'useless-files', 
    'file.txt', 
    'test 1'
);

$filePath2 = Yii::$app->uploads->saveContent(
    'useless-files', 
    'file.txt', 
    'test 2', 
    \herroffizier\yii2um\UploadManager::STRATEGY_RENAME
);

echo "$filePath1, $filePath2";

This code will also complete correctly and output useless-files/75/file.txt, useless-files/75/file-1.txt. As you may see, second file has an index 1 at the end of its name., (*23)

The Versions

31/03 2016

dev-develop

dev-develop

Simplifies file storing in Yii 2.

  Sources   Download

The Requires

 

The Development Requires

extension yii2 upload yii yii 2

31/03 2016

dev-master

9999999-dev

Simplifies file storing in Yii 2.

  Sources   Download

The Requires

 

The Development Requires

extension yii2 upload yii yii 2

31/03 2016

1.0.0

1.0.0.0

Simplifies file storing in Yii 2.

  Sources   Download

The Requires

 

The Development Requires

extension yii2 upload yii yii 2

26/10 2015

0.1

0.1.0.0

Simplifies file storing in Yii 2.

  Sources   Download

The Requires

 

The Development Requires

extension yii2 upload yii yii 2