PHPackages                             weison-tech/yii2-file-kit - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. weison-tech/yii2-file-kit

ActiveYii2-extension

weison-tech/yii2-file-kit
=========================

Yii2 file upload and storage kit

1.2.0(9y ago)0681BSD-3-ClausePHP

Since Sep 4Pushed 9y ago1 watchersCompare

[ Source](https://github.com/weison-tech/yii2-file-kit)[ Packagist](https://packagist.org/packages/weison-tech/yii2-file-kit)[ RSS](/packages/weison-tech-yii2-file-kit/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (6)Versions (19)Used By (0)

![Packagist](https://camo.githubusercontent.com/9569ce104966ff31cefa1919c4366e10e8a7a3fdde02146234d25df139b24c7f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f74726e74762f796969322d66696c652d6b69742e737667)[![Dependency Status](https://camo.githubusercontent.com/b5b299fb99ba65d51919e976d2671fdf22b967ac4e97c20009afcf914ec5fa76/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f7068702f74726e74763a796969322d66696c652d6b69742f62616467652e737667)](https://www.versioneye.com/php/trntv:yii2-file-kit/2.0.0)

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](https://github.com/blueimp/jQuery-File-Upload))
- Component for storing files (built on top of [flysystem](https://github.com/thephpleague/flysystem))
- Actions to download, delete, and view (download) files
- Behavior for saving files in the model and delete files when you delete a model

Here you can see list of available [filesystem adapters](https://github.com/thephpleague/flysystem#adapters)

Demo
----

[](#demo)

Since file kit is a part of [yii2-starter-kit](https://github.com/trntv/yii2-starter-kit) it's demo can be found in starter kit demo [here](http://backend.yii2-starter-kit.terentev.net/article/create).

Installation
============

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require trntv/yii2-file-kit

```

or add

```
"trntv/yii2-file-kit": "@stable"

```

to the require section of your `composer.json` file.

File Storage
============

[](#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.

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

There are several ways to configure `trntv\filekit\Storage` to work with `flysystem`.

Using Closure
-------------

[](#using-closure)

```
'fileStorage'=>[
    ...
    'filesystem'=> function() {
        $adapter = new \League\Flysystem\Adapter\Local('some/path/to/storage');
        return new League\Flysystem\Filesystem($adapter);
    }
]
```

Using filesystem builder
------------------------

[](#using-filesystem-builder)

- Create a builder class that implements `trntv\filekit\filesystem\FilesystemBuilderInterface` and implement method `build` which returns filesystem object. See `examples/`
- Add to your configuration:

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

Read more about flysystem at

Using third-party extensions
----------------------------

[](#using-third-party-extensions)

- Create filesystem component (example uses `creocoder/yii2-flysystem`)

```
'components' => [
    ...
    'fs' => [
        'class' => 'creocoder\flysystem\LocalFilesystem',
        'path' => '@webroot/files'
    ],
    ...
]
```

- Set filesystem component name in storage configuration:

```
'components' => [
    ...
    'fileStorage'=>[
        'filesystemComponent'=> 'fs'
    ],
    ...
]
```

Actions
=======

[](#actions)

File Kit contains several Actions to work with uploads.

### Upload Action

[](#upload-action)

Designed to save the file uploaded by the widget

```
public function actions(){
    return [
           'upload'=>[
               'class'=>'trntv\filekit\actions\UploadAction',
               //'deleteRoute' => 'my-custom-delete', // my custom delete action for deleting just uploaded files(not yet saved)
               //'fileStorage' => 'myfileStorage', // my custom fileStorage from configuration
               'multiple' => true,
               'disableCsrf' => true,
               'responseFormat' => Response::FORMAT_JSON,
               'responsePathParam' => 'path',
               'responseBaseUrlParam' => 'base_url',
               'responseUrlParam' => 'url',
               'responseDeleteUrlParam' => 'delete_url',
               'responseMimeTypeParam' => 'type',
               'responseNameParam' => 'name',
               'responseSizeParam' => 'size',
               'deleteRoute' => 'delete',
               'fileStorage' => 'fileStorage', // Yii::$app->get('fileStorage')
               'fileStorageParam' => 'fileStorage', // ?fileStorage=someStorageComponent
               'sessionKey' => '_uploadedFiles',
               'allowChangeFilestorage' => false,
               '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

### Delete Action

[](#delete-action)

```
public function actions(){
    return [
       'delete'=>[
           'class'=>'trntv\filekit\actions\DeleteAction',
           //'fileStorage' => 'fileStorageMy', // my custom fileStorage from configuration(such as in the upload action)
       ]
    ];
}
```

See additional settings in the corresponding class

### View (Download) Action

[](#view-download-action)

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

See additional settings in the corresponding class

Upload Widget
=============

[](#upload-widget)

Standalone usage

```
echo \trntv\filekit\widget\Upload::widget([
    'model' => $model,
    'attribute' => 'files',
    'url' => ['upload'],
    'sortable' => true,
    'maxFileSize' => 10 * 1024 * 1024, // 10Mb
    'minFileSize' => 1 * 1024 * 1024, // 1Mb
    'maxNumberOfFiles' => 3 // default 1,
    'acceptFileTypes' => new JsExpression('/(\.|\/)(gif|jpe?g|png)$/i'),
    'showPreviewFilename' => false,
    'clientOptions' => [ ...other blueimp options... ]
]);
```

With ActiveForm

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

Upload Widget events
--------------------

[](#upload-widget-events)

Upload widget trigger some of built-in blueimp events:

- start
- fail
- done
- always

You can use them directly or add your custom handlers in options:

```
'clientOptions' => [
    'start' => new JsExpression('function(e, data) { ... do something ... }'),
    'done' => new JsExpression('function(e, data) { ... do something ... }'),
    'fail' => new JsExpression('function(e, data) { ... do something ... }'),
    'always' => new JsExpression('function(e, data) { ... do something ... }'),
 ]
```

UploadBehavior
==============

[](#uploadbehavior)

This behavior is designed to save uploaded files in the corresponding relation.

Somewhere in model:

For multiple files

```
 public function behaviors()
 {
    return [
        'file' => [
            'class' => 'trntv\filekit\behaviors\UploadBehavior',
            'filesStorage' => 'myfileStorage', // my custom fileStorage from configuration(for properly remove the file from disk)
            'multiple' => true,
            'attribute' => 'files',
            'uploadRelation' => 'uploadedFiles',
            'pathAttribute' => 'path',
            'baseUrlAttribute' => 'base_url',
            'typeAttribute' => 'type',
            'sizeAttribute' => 'size',
            'nameAttribute' => 'name',
            'orderAttribute' => 'order'
        ],
    ];
 }
```

For single file upload

```
 public function behaviors()
 {
     return [
          'file' => [
              'class' => 'trntv\filekit\behaviors\UploadBehavior',
              'filesStorage' => 'fileStorageMy', // my custom fileStorage from configuration(for properly remove the file from disk)
              'attribute' => 'file',
              'pathAttribute' => 'path',
              'baseUrlAttribute' => 'base_url',
               ...
          ],
      ];
 }
```

See additional settings in the corresponding class.

Validation
==========

[](#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](https://github.com/blueimp/jQuery-File-Upload/wiki/Options#validation-options) about available options.

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

Tips
====

[](#tips)

Adding watermark
----------------

[](#adding-watermark)

Install `intervention/image` library

```
composer require intervention/image

```

Edit your upload actions as so

```
public function actions(){
    return [
           'upload'=>[
               'class'=>'trntv\filekit\actions\UploadAction',
               ...
               'on afterSave' => function($event) {
                    /* @var $file \League\Flysystem\File */
                    $file = $event->file;

                    // create new Intervention Image
                    $img = Intervention\Image\ImageManager::make($file->read());

                    // insert watermark at bottom-right corner with 10px offset
                    $img->insert('public/watermark.png', 'bottom-right', 10, 10);

                    // save image
                    $file->put($img->encode());
               }
               ...
           ]
       ];
}

```

###  Health Score

32

—

LowBetter than 74% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor6

6 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~50 days

Recently: every ~118 days

Total

18

Last Release

3355d ago

Major Versions

0.4 → 1.0.02015-03-24

### Community

Maintainers

![](https://www.gravatar.com/avatar/ad2af46e2068dc147a65dd1113be09f39b8da08b7e1e43179152f022ff027bd4?d=identicon)[xiaomalover](/maintainers/xiaomalover)

---

Top Contributors

[![Eireen](https://avatars.githubusercontent.com/u/2239743?v=4)](https://github.com/Eireen "Eireen (3 commits)")[![1allen](https://avatars.githubusercontent.com/u/597599?v=4)](https://github.com/1allen "1allen (3 commits)")[![ferluk](https://avatars.githubusercontent.com/u/633999?v=4)](https://github.com/ferluk "ferluk (2 commits)")[![trntv](https://avatars.githubusercontent.com/u/1162056?v=4)](https://github.com/trntv "trntv (2 commits)")[![keltstr](https://avatars.githubusercontent.com/u/4305053?v=4)](https://github.com/keltstr "keltstr (1 commits)")[![kharalampidi](https://avatars.githubusercontent.com/u/11192488?v=4)](https://github.com/kharalampidi "kharalampidi (1 commits)")[![kroshilin](https://avatars.githubusercontent.com/u/10268995?v=4)](https://github.com/kroshilin "kroshilin (1 commits)")[![senguttuvang](https://avatars.githubusercontent.com/u/1772791?v=4)](https://github.com/senguttuvang "senguttuvang (1 commits)")[![thecodeholic](https://avatars.githubusercontent.com/u/4627922?v=4)](https://github.com/thecodeholic "thecodeholic (1 commits)")[![tol17](https://avatars.githubusercontent.com/u/6351270?v=4)](https://github.com/tol17 "tol17 (1 commits)")[![verstoff](https://avatars.githubusercontent.com/u/3609218?v=4)](https://github.com/verstoff "verstoff (1 commits)")[![xBazilio](https://avatars.githubusercontent.com/u/4119114?v=4)](https://github.com/xBazilio "xBazilio (1 commits)")[![xiaomalover](https://avatars.githubusercontent.com/u/10594451?v=4)](https://github.com/xiaomalover "xiaomalover (1 commits)")[![yanhuixie](https://avatars.githubusercontent.com/u/1049615?v=4)](https://github.com/yanhuixie "yanhuixie (1 commits)")[![Anton-Am](https://avatars.githubusercontent.com/u/2063230?v=4)](https://github.com/Anton-Am "Anton-Am (1 commits)")[![chinaphp](https://avatars.githubusercontent.com/u/520827?v=4)](https://github.com/chinaphp "chinaphp (1 commits)")[![chizdrel](https://avatars.githubusercontent.com/u/4534533?v=4)](https://github.com/chizdrel "chizdrel (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/weison-tech-yii2-file-kit/health.svg)

```
[![Health](https://phpackages.com/badges/weison-tech-yii2-file-kit/health.svg)](https://phpackages.com/packages/weison-tech-yii2-file-kit)
```

###  Alternatives

[mihaildev/yii2-elfinder

Yii2 ElFinder

171650.8k49](/packages/mihaildev-yii2-elfinder)[edofre/yii2-fullcalendar

Yii2 widget for fullcalendar

2761.2k1](/packages/edofre-yii2-fullcalendar)[limion/yii2-jquery-fileupload-widget

Blueimp file upload widget for Yii2

1224.3k](/packages/limion-yii2-jquery-fileupload-widget)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
