2017 © Pedro Peláez
 

yii2-extension yii2-file-kit

Yii2 file upload and storage kit plus, add JCrop and resize

image

liv/yii2-file-kit

Yii2 file upload and storage kit plus, add JCrop and resize

  • Monday, August 24, 2015
  • by Pavle Lee
  • Repository
  • 1 Watchers
  • 3 Stars
  • 11 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 82 Forks
  • 0 Open issues
  • 12 Versions
  • 0 % Grown

The README.md

Packagist Dependency Status, (*1)

This kit is designed to automate routine processes of uploading files, their saving and storage. It includes: - File upload widget (based on Blueimp File Upload) - Component for storing files (built on top of flysystem) - Actions to download, delete, and view (download) files - Behavior for saving files in the model and delete files when you delete a model, (*2)

Demo

Since file kit is a part of yii2-starter-kit it's demo can be found in starter kit demo here., (*3)

File Storage

To work with the File Kit you need to configure FileStorage first. This component is a layer of abstraction over the filesystem - Its main task to take on the generation of a unique name for each file and trigger corresponding events., (*4)

'fileStorage'=>[
    'class' => 'trntv\filekit\Storage',
    'baseUrl' => '@web/uploads'
    'filesystem'=> ...
        // OR
    'filesystemComponent' => ...    
],

There are several ways to configure Storage to work with flysystem., (*5)

  1. Create a builder class that implements trntv\filekit\filesystem\FilesystemBuilderInterface and implement methodbuild which returns filesystem object Example:
namespace app\components;

use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local as Adapter;
use trntv\filekit\filesystem\FilesystemBuilderInterface;

class LocalFlysystemBuilder implements FilesystemBuilderInterface
{
    public $path;

    public function build()
    {
        $adapter = new Local(\Yii::getAlias($this->path));
        return new Filesystem($adapter);
    }
}

Configuration:, (*6)

'fileStorage'=>[
    ...
    'filesystem'=> [
        'class' => 'app\components\FilesystemBuilder',
        'path' => '@webroot/uploads'
        ...
    ]
]

Read more about flysystem at http://flysystem.thephpleague.com/, (*7)

Then you can use it like this:, (*8)

$file = UploadedFile::getInstanceByName('file');
Yii::$app->fileStorage->save($file); // method will return new path inside filesystem
$files = UploadedFile::getInstancesByName('files');
Yii::$app->fileStorage->saveAll($files);
  1. Use third-party extensions, creocoder/yii2-flysystem for example, and provide a name of the filesystem component in filesystemComponent Configuration:
'fs' => [
    'class' => 'creocoder\flysystem\LocalFilesystem',
    'path' => '@webroot/files'
    ...
],
'fileStorage'=>[
    ...
    'filesystemComponent'=> 'fs'
],

Actions

File Kit contains several Actions to work with uploads., (*9)

Upload Action

Designed to save the file uploaded by the widget, (*10)

public function actions(){
    return [
           'upload'=>[
               'class'=>'trntv\filekit\actions\UploadAction',
               'validationRules' => [
                    ...
               ],
               'on afterSave' => function($event) {
                    /* @var $file \League\Flysystem\File */
                    $file = $event->file
                    // do something (resize, add watermark etc)
               }
           ]
       ];
}

See additional settings in the corresponding class, (*11)

Delete Action

public function actions(){
    return [
       'delete'=>[
           'class'=>'trntv\filekit\actions\DeleteAction',
       ]
    ];
}

See additional settings in the corresponding class, (*12)

View (Download) Action

public function actions(){
    return [
       'view'=>[
           'class'=>'trntv\filekit\actions\ViewAction',
       ]
    ];
}

See additional settings in the corresponding class, (*13)

Upload Widget

Standalone usage, (*14)

echo \trntv\filekit\widget\Upload::widget([
    'model'=>$model,
    'attribute'=>'files',
    'url'=>['upload'],
    'sortable'=>true,
    'maxFileSize'=>10 * 1024 * 1024, 
    'maxNumberOfFiles'=>3 // default 1
]);

With ActiveForm, (*15)

echo $form->field($model, 'files')->widget(
    '\trntv\filekit\widget\Upload',
    [
        'url'=>['upload'],
        'sortable'=>true,
        'maxFileSize'=>10 * 1024 * 1024, // 10 MiB
        'maxNumberOfFiles'=>3 // default 1
    ]
);

UploadBehavior

This behavior is designed to save uploaded files in the corresponding relation., (*16)

Somewhere in model:, (*17)

For multiple files, (*18)

 public function behaviors()
 {
    return [
        'file' => [
            'class' => 'trntv\filekit\behaviors\UploadBehavior',
            'multiple' => true,
            'attribute' => 'files',
            'filesRelation' => 'uploadedFiles',
            'pathAttribute' => 'path',
            'baseUrlAttribute' => 'base_url',
            'typeAttribute' => 'type',
            'sizeAttribute' => 'size',
            'nameAttribute' => 'name',
            'orderAttribute' => 'order'
        ],
    ];
 }

For single file upload, (*19)

 public function behaviors()
 {
     return [
          'file' => [
              'class' => 'trntv\filekit\behaviors\UploadBehavior',
              'attribute' => 'file',
              'pathAttribute' => 'path',
              'baseUrlAttribute' => 'base_url',
               ...
          ],
      ];
 }

See additional settings in the corresponding class., (*20)

Validation

There are two ways you can perform validation over uploads. On the client side validation is performed by Blueimp File Upload. Here is documentation about available options., (*21)

On the server side validation is performed by [[yii\web\UploadAction]], where you can configure validation rules for [[yii\base\DynamicModel]] that will be used in validation process, (*22)

The Versions

24/08 2015

dev-master

9999999-dev

Yii2 file upload and storage kit plus, add JCrop and resize

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Liv

24/07 2015

1.2.0

1.2.0.0

Yii2 file upload and storage kit plus, add JCrop and resize

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Liv

01/06 2015

1.1.1

1.1.1.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

25/05 2015

1.1.0

1.1.0.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

14/05 2015

1.0.3

1.0.3.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

26/04 2015

1.0.2

1.0.2.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

07/04 2015

1.0.1

1.0.1.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

24/03 2015

1.0.0

1.0.0.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

13/01 2015

0.4

0.4.0.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

by Eugine Terentev

24/09 2014

0.3

0.3.0.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

by Eugine Terentev

04/09 2014

0.1

0.1.0.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

by Eugine Terentev

04/09 2014

0.2

0.2.0.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

by Eugine Terentev