PHPackages                             coderius/yii2-upload-file-behavior - 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. coderius/yii2-upload-file-behavior

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

coderius/yii2-upload-file-behavior
==================================

yii2 hit counter

v1.0(5y ago)1149MITPHPPHP &gt;=5.6.0

Since Aug 18Pushed 5y ago1 watchersCompare

[ Source](https://github.com/coderius/yii2-upload-file-behavior)[ Packagist](https://packagist.org/packages/coderius/yii2-upload-file-behavior)[ RSS](/packages/coderius-yii2-upload-file-behavior/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (4)Versions (4)Used By (0)

Yii2 upload file behavior
=========================

[](#yii2-upload-file-behavior)

[![Software License](https://camo.githubusercontent.com/57f3781bfdca1ffc75b5497cd0731d9910d5b82b1b6f46e26db41cdeabd7b9ca/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f636f6465726975732f796969322d75706c6f61642d66696c652d6265686176696f72)](LICENSE.md)[![Latest Version](https://camo.githubusercontent.com/34c8f8112f8c157c8eb55f59404d533062b267374c898256083ecd02205074c5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f7461672f636f6465726975732f796969322d75706c6f61642d66696c652d6265686176696f722e7376673f7374796c653d666c61742d737175617265266c6162656c3d72656c65617365)](https://github.com/coderius/yii2-upload-file-behavior/tags)[![Code Coverage](https://camo.githubusercontent.com/0de7679794b50bbea349c90e157d2318ccdd14855279376875097ba58711b43c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f636f6465726975732f796969322d75706c6f61642d66696c652d6265686176696f722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/coderius/yii2-upload-file-behavior/?branch=master)[![Code Intelligence Status](https://camo.githubusercontent.com/8b11db39a4aaf79ec1dd071faccec3e626fddf0870cfc4578977fe068beee5f5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f636f6465726975732f796969322d75706c6f61642d66696c652d6265686176696f722f6261646765732f636f64652d696e74656c6c6967656e63652e7376673f623d6d6173746572)](https://scrutinizer-ci.com/code-intelligence)[![Code Quality](https://camo.githubusercontent.com/74e9f1326dc36376e24ff7afb3250f306a6dc6dd3122d494aa6d4601d5c679cf/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f7175616c6974792f672f636f6465726975732f796969322d75706c6f61642d66696c652d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/coderius/yii2-upload-file-behavior/?branch=master)[![Build Status](https://camo.githubusercontent.com/4825bb29521ee0d30e08c03fa28417933330da28557ec61e2b10085f54c011e1/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f636f6465726975732f796969322d75706c6f61642d66696c652d6265686176696f722f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/coderius/yii2-upload-file-behavior/build-status/master)[![Total Downloads](https://camo.githubusercontent.com/598f1dd92d1fda6fe38fe2272d6ad48365cb88af85038cbb54f368e10f7ed3a3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f6465726975732f796969322d75706c6f61642d66696c652d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/coderius/yii2-upload-file-behavior)

About
-----

[](#about)

Yii2 upload file behavior - simple wey to upload images and files to server. No need anymore wrote tonn of code in controller and else testing it by houers. As a result - saving time and labor costs for uploading files to the site. Only needed upload extention from github and past some less code to model class (\\yii\\db\\ActiveRecord) where needed hendler uploading files. More on this below.

Installation
------------

[](#installation)

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

First download extention. Run the command in the terminal:

```
composer require "coderius/yii2-upload-file-behavior"

```

or add in composer.json

```
"coderius/yii2-upload-file-behavior": "^1.0"

```

and run `composer update`

Usage
-----

[](#usage)

This extention created for usage in \\yii\\db\\ActiveRecord model classes.

Configyration behavior.

- **$nameOfAttributeFile** = (string) default name 'file'. Virtual attribute for uploading file instance from file systrem.
- **$nameOfAttributeStorage** = (string) default name 'face\_img'. Attribute for saving path to uploaded file in db.
- **$newFileName** = (string) name which is assigned to uploaded file
- **$directories** = (array) configs to upload folder and upload hendlers.Сonsists of separate arrays. Each array contains settings for the path to the target folder and a handler for uploading files to this folder like 'path' and 'handler' **-'path'** - contains path to target folder **-'hendler'** - Processes the downloaded file and saves to the specified in param 'path' location.

This extention created for usage in \\yii\\db\\ActiveRecord model classes.

- So, first in model class put namespace to yii2-upload-file-behavior.
- Create public variable $file for loading file from filesystem.
- The database must have an attribute to store the file path. In example below it is 'img\_src' attribute (marked like save in public function rules())
- Then past needed configs behaviors() method like in example.

**!Note.** *Don't forget to include the dependency namespaces.*

**Example**

```
    namespase your/models;

    use coderius\yii2UploadFileBehavior\UploadFileBehavior;
    use yii\imagine\Image;
    use Imagine\Image\Point;
    use Imagine\Image\Box;

    class YourModel extends \yii\db\ActiveRecord
    {
        public $file;

        //'img_src' - attribute to save path to file in db
        public function rules()
        {
            return [
                [['img_src'], 'safe'],
        }

        ...

            public function behaviors()
            {
                return [
                    //Another behaviors
                    //...

                    'uploadFileBehavior' => [
                        'class' => UploadFileBehavior::className(),
                        'nameOfAttributeStorage' => 'img_src',
                        'directories' => [

                            [
                                'path' => function($attributes){
                                    return \Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/big/');
                                },
                                'hendler' => function($fileTempName, $newFilePath){
                                    Image::thumbnail($fileTempName, 900, 900*2/3)
                                    ->copy()
                                    ->crop(new Point(0, 0), new Box(900, 900*2/3))
                                    ->save($newFilePath, ['quality' => 80]);
                                    sleep(1);
                                }
                            ],
                            [
                                'path' => function($attributes){
                                    return \Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/middle/');
                                },
                                'hendler' => function($fileTempName, $newFilePath){
                                    Image::thumbnail($fileTempName, 400, 400*2/3)
                                    ->save($newFilePath, ['quality' => 80]);
                                    sleep(1);
                                }
                            ],
                            [
                                'path' => function($attributes){
                                    return \Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/small/');
                                },
                                'hendler' => function($fileTempName, $newFilePath){
                                    Image::thumbnail($fileTempName, 150, 150*2/3)
                                    ->save($newFilePath, ['quality' => 80]);
                                    sleep(1);
                                }
                            ],
                        ]
                    ],

                ];
            }

        ...
    }

```

Short simple config:

```
'uploadFileBehavior' => [
    'class' => UploadFileBehavior::className(),
    'nameOfAttributeStorage' => 'img_src',
    'newFileName' => 'image-123',
    'targets' => [
        [
            'path' => '@uploadsPath',
            'hendler' => [
                'type' => UploadFileBehavior::TYPE_IMAGE,
                'config' => [
                    'size' => [
                        'width' => 400,
                        'height'=> 400*2/3
                    ],
                    'quality' => 80
                ]
            ]
        ]
    ]
]

```

In this case allowed params is: 'size' &amp; 'quality'.

Additional actions:
-------------------

[](#additional-actions)

1. Create aliases to target folders for saving uploaded files.
2. Create target folders in 'frontend/web' dirrectory like example.
3. Don't forget create vertual attribute. If it named like '$file', then no need set config to 'nameOfAttributeFile'(default = (string)'file').

Testing
-------

[](#testing)

Run tests in extention folder.

```
$ ./vendor/bin/phpunit
```

Note! For running all tests needed upload all dependencies by composer. If tested single extention, then run command from root directory where located extention:

```
composer update

```

When all dependencies downloaded run all tests in terminal from root folder:

```
./vendor/bin/phpunit tests

```

Or for only unit:

```
./vendor/bin/phpunit --testsuite Unit

```

If extention tested in app, then set correct path to phpunit and run some commands.

Credits
-------

[](#credits)

- [Sergio Coderius](https://github.com/coderius)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

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.

###  Release Activity

Cadence

Every ~8 days

Total

2

Last Release

2090d ago

Major Versions

v0.1 → v1.02020-08-26

### Community

Maintainers

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

---

Top Contributors

[![coderius](https://avatars.githubusercontent.com/u/36597893?v=4)](https://github.com/coderius "coderius (30 commits)")

---

Tags

behaviorfileimageupload-fileupload-imagesupload-picturesuploaderyii2yii2-behavioryii2-behaviors-upload-fileyii2-extensionyii2-modulesyii2-save-fileyii2-upload-fileyii2-upload-imageimagefileuploadyii2Behavioryii2-extensionyii2 modulesyii2 upload file

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/coderius-yii2-upload-file-behavior/health.svg)

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

###  Alternatives

[mohorev/yii2-upload-behavior

Upload behavior for Yii 2

128265.5k9](/packages/mohorev-yii2-upload-behavior)[yii-dream-team/yii2-upload-behavior

Yii2 file/image upload behavior for ActiveRecord

79207.1k16](/packages/yii-dream-team-yii2-upload-behavior)[liyunfang/yii2-upload-behavior

Upload behavior for Yii 2

161.7k](/packages/liyunfang-yii2-upload-behavior)[demi/image

Yii2 behavior for upload image to model

2214.7k](/packages/demi-image)[sjaakp/yii2-illustrated-behavior

ActiveRecord Behavior with associated Widget for Yii2.

423.1k](/packages/sjaakp-yii2-illustrated-behavior)

PHPackages © 2026

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