PHPackages                             yiioverflow/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. yiioverflow/yii2-file-kit

ActiveYii2-extension

yiioverflow/yii2-file-kit
=========================

Yii2 file upload and storage kit

3398PHP

Since Jun 4Pushed 7y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

[![Dependency Status](https://camo.githubusercontent.com/25ef2277ebfd78e91994a42d0b6adaba6f7908c46688ed2b5a30c8c89697d8a1/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f7068702f7969696f766572666c6f773a796969322d66696c652d6b69742f62616467652e737667)](https://www.versioneye.com/php/yiioverflow: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

Demo ---- [coming soon](http://yiioverflow.com)

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

[](#installation)

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

Either run

```
composer require yiioverflow/yii2-file-kit  "@dev"

```

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' => 'yiioverflow\filekit\Storage',
    'baseUrl' => '@web/uploads'
    'filesystem'=> ...
        // OR
    'filesystemComponent' => ...
],
```

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

1. Create a builder class that implements `yiioverflow\filekit\filesystem\FilesystemBuilderInterface` and implement method` build`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 yiioverflow\filekit\filesystem\FilesystemBuilderInterface;

class LocalFlysystemBuilder implements FilesystemBuilderInterface
{
    public $path;

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

Configuration:

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

Read more about flysystem at

Then you can use it like this:

```
$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);
```

2. 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
=======

[](#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'=>'yiioverflow\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

### Delete Action

[](#delete-action)

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

See additional settings in the corresponding class

### View (Download) Action

[](#view-download-action)

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

See additional settings in the corresponding class

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

[](#upload-widget)

Standalone usage

```
echo \yiioverflow\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'),
    'clientOptions' => [ ...other blueimp options... ]
]);
```

With ActiveForm

```
echo $form->field($model, 'files')->widget(
    '\yiioverflow\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' => 'function(e, data) { ... do something ... }',
    'done' => 'function(e, data) { ... do something ... }',
    'fail' => 'function(e, data) { ... do something ... }',
    'always' => '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' => 'yiioverflow\filekit\behaviors\UploadBehavior',
            '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' => 'yiioverflow\filekit\behaviors\UploadBehavior',
              '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'=>'yiioverflow\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

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2894563?v=4)[Roopz](/maintainers/yiioverflow)[@yiioverflow](https://github.com/yiioverflow)

---

Top Contributors

[![yiioverflow](https://avatars.githubusercontent.com/u/2894563?v=4)](https://github.com/yiioverflow "yiioverflow (8 commits)")

### Embed Badge

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

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

PHPackages © 2026

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