PHPackages                             yii2-starter-kit/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. [File &amp; Storage](/categories/file-storage)
4. /
5. yii2-starter-kit/yii2-file-kit

ActiveYii2-extension[File &amp; Storage](/categories/file-storage)

yii2-starter-kit/yii2-file-kit
==============================

Yii2 file upload and storage kit

2.2.0(2mo ago)151216.8k—9.9%102[19 issues](https://github.com/yii-starter-kit/yii2-file-kit/issues)6BSD-3-ClausePHPCI passing

Since Sep 4Pushed 2mo ago20 watchersCompare

[ Source](https://github.com/yii-starter-kit/yii2-file-kit)[ Packagist](https://packagist.org/packages/yii2-starter-kit/yii2-file-kit)[ RSS](/packages/yii2-starter-kit-yii2-file-kit/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (11)Versions (33)Used By (6)

[![GitHub Workflow Status](https://camo.githubusercontent.com/ab05354ec67a716b841a0f3d0eca2f6bf31ce78caafec82951111b6740a43b9b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f796969322d737461727465722d6b69742f796969322d66696c652d6b69742f5465737473)](https://camo.githubusercontent.com/ab05354ec67a716b841a0f3d0eca2f6bf31ce78caafec82951111b6740a43b9b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f796969322d737461727465722d6b69742f796969322d66696c652d6b69742f5465737473) [![Packagist Version (custom server)](https://camo.githubusercontent.com/16b3a008b4ea806a5bb6d8cc45c3e0967e64e77ac852bb62a612b3cd36837c2a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f796969322d737461727465722d6b69742f796969322d66696c652d6b6974)](https://camo.githubusercontent.com/16b3a008b4ea806a5bb6d8cc45c3e0967e64e77ac852bb62a612b3cd36837c2a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f796969322d737461727465722d6b69742f796969322d66696c652d6b6974) [![Packagist](https://camo.githubusercontent.com/03d8a5003735ab502176fb0f4eb8cc0703cd3b52d0cd7d663e16225f7cb5b608/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f796969322d737461727465722d6b69742f796969322d66696c652d6b6974)](https://camo.githubusercontent.com/03d8a5003735ab502176fb0f4eb8cc0703cd3b52d0cd7d663e16225f7cb5b608/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f796969322d737461727465722d6b69742f796969322d66696c652d6b6974)

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/yii2-starter-kit/yii2-starter-kit) it's demo can be found in starter kit demo [here](http://backend.yii2-starter-kit.terentev.net/content/article/create).

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

[](#installation)

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

Either run

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

```

or add

```
"yii2-starter-kit/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',
    'useDirindex' => true,
    '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'],
    'uploadPath' => 'subfolder', // optional, for storing files in storage subfolder
    'sortable' => true,
    'maxFileSize' => 10 * 1024 * 1024, // 10Mb
    'minFileSize' => 1 * 1024 * 1024, // 1Mb
    'maxNumberOfFiles' => 3, // default 1,
    'acceptFileTypes' => new \yii\web\JsExpression('/(\.|\/)(gif|jpe?g|png)$/i'),
    'showPreviewFilename' => false,
    'editFilename' => false,
    'clientOptions' => [/* ...other blueimp options... */]
]);
```

Standalone usage - without model

```
echo \trntv\filekit\widget\Upload::widget([
    'name' => 'filename',
    'hiddenInputId' => 'filename', // must for not use model
    'url' => ['upload'],
    'uploadPath' => 'subfolder', // optional, for storing files in storage subfolder
    'sortable' => true,
    'maxFileSize' => 10 * 1024 * 1024, // 10Mb
    'minFileSize' => 1 * 1024 * 1024, // 1Mb
    'maxNumberOfFiles' => 3, // default 1,
    'acceptFileTypes' => new \yii\web\JsExpression('/(\.|\/)(gif|jpe?g|png)$/i'),
    'showPreviewFilename' => false,
    'editFilename' => false,
    'clientOptions' => [/* ...other blueimp options... */]
]);
```

With ActiveForm

```
echo $form->field($model, 'files')->widget(
    '\trntv\filekit\widget\Upload',
    [
        'url' => ['upload'],
        'uploadPath' => 'subfolder', // optional, for storing files in storage subfolder
        '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

65

—

FairBetter than 99% of packages

Maintenance84

Actively maintained with recent releases

Popularity53

Moderate usage in the ecosystem

Community40

Growing community involvement

Maturity74

Established project with proven stability

 Bus Factor5

5 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 ~145 days

Recently: every ~538 days

Total

30

Last Release

70d ago

Major Versions

0.4 → 1.0.02015-03-24

1.3.1 → 2.0.02018-11-15

### Community

Maintainers

![](https://www.gravatar.com/avatar/9a67acd28d366ff328006f3e62c314993301a3742af6dbf0da18693cda04c9c6?d=identicon)[trntv](/maintainers/trntv)

![](https://www.gravatar.com/avatar/3fd1ace71516f0450becfde434768c796da0e314b6a20ed4900ff3ee0f1cc081?d=identicon)[XzAeRo](/maintainers/XzAeRo)

---

Top Contributors

[![XzAeRo](https://avatars.githubusercontent.com/u/1656037?v=4)](https://github.com/XzAeRo "XzAeRo (22 commits)")[![z1bun](https://avatars.githubusercontent.com/u/13135730?v=4)](https://github.com/z1bun "z1bun (6 commits)")[![trntv](https://avatars.githubusercontent.com/u/1162056?v=4)](https://github.com/trntv "trntv (5 commits)")[![ztdan4ik](https://avatars.githubusercontent.com/u/14123515?v=4)](https://github.com/ztdan4ik "ztdan4ik (4 commits)")[![1allen](https://avatars.githubusercontent.com/u/597599?v=4)](https://github.com/1allen "1allen (3 commits)")[![Eireen](https://avatars.githubusercontent.com/u/2239743?v=4)](https://github.com/Eireen "Eireen (3 commits)")[![trancesmile](https://avatars.githubusercontent.com/u/1496807?v=4)](https://github.com/trancesmile "trancesmile (3 commits)")[![xxcarleonexx](https://avatars.githubusercontent.com/u/12188919?v=4)](https://github.com/xxcarleonexx "xxcarleonexx (3 commits)")[![sircovsw](https://avatars.githubusercontent.com/u/3596745?v=4)](https://github.com/sircovsw "sircovsw (2 commits)")[![thecodeholic](https://avatars.githubusercontent.com/u/4627922?v=4)](https://github.com/thecodeholic "thecodeholic (2 commits)")[![mehrna](https://avatars.githubusercontent.com/u/6211768?v=4)](https://github.com/mehrna "mehrna (2 commits)")[![kharalampidi](https://avatars.githubusercontent.com/u/11192488?v=4)](https://github.com/kharalampidi "kharalampidi (2 commits)")[![ferluk](https://avatars.githubusercontent.com/u/633999?v=4)](https://github.com/ferluk "ferluk (2 commits)")[![krissss](https://avatars.githubusercontent.com/u/10680903?v=4)](https://github.com/krissss "krissss (2 commits)")[![verstoff](https://avatars.githubusercontent.com/u/3609218?v=4)](https://github.com/verstoff "verstoff (1 commits)")[![vkovrizhkin](https://avatars.githubusercontent.com/u/25133638?v=4)](https://github.com/vkovrizhkin "vkovrizhkin (1 commits)")[![vuongxuongminh](https://avatars.githubusercontent.com/u/38932626?v=4)](https://github.com/vuongxuongminh "vuongxuongminh (1 commits)")[![xavsio4](https://avatars.githubusercontent.com/u/1721936?v=4)](https://github.com/xavsio4 "xavsio4 (1 commits)")[![xBazilio](https://avatars.githubusercontent.com/u/4119114?v=4)](https://github.com/xBazilio "xBazilio (1 commits)")[![bitsnaps](https://avatars.githubusercontent.com/u/1217741?v=4)](https://github.com/bitsnaps "bitsnaps (1 commits)")

---

Tags

hacktoberfest

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[league/flysystem-aws-s3-v3

AWS S3 filesystem adapter for Flysystem.

1.6k263.6M790](/packages/league-flysystem-aws-s3-v3)[unisharp/laravel-filemanager

A file upload/editor intended for use with Laravel 5 to 10 and CKEditor / TinyMCE

2.2k3.3M74](/packages/unisharp-laravel-filemanager)[league/flysystem-sftp-v3

SFTP filesystem adapter for Flysystem.

6129.6M91](/packages/league-flysystem-sftp-v3)[azure-oss/storage-blob-flysystem

Flysystem adapter for Azure Storage PHP

29936.0k10](/packages/azure-oss-storage-blob-flysystem)[jerodev/flysystem-v3-smb-adapter

SMB adapter for Flysystem v3

1289.9k1](/packages/jerodev-flysystem-v3-smb-adapter)[dgtlss/capsule

A Laravel package for backing up databases and files to external sources with notifications

194.3k](/packages/dgtlss-capsule)

PHPackages © 2026

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