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

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

paulzi/yii2-file-behavior
=========================

File store and image thumbs behavior for Yii2

v1.4.2(5y ago)85752[1 PRs](https://github.com/paulzi/yii2-file-behavior/pulls)MITPHPPHP &gt;=5.4.0

Since Mar 29Pushed 5y ago3 watchersCompare

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

READMEChangelogDependencies (2)Versions (21)Used By (0)

Yii2 file and image helper
==========================

[](#yii2-file-and-image-helper)

File store and image thumbs behavior for Yii2.

[![Packagist Version](https://camo.githubusercontent.com/9baf3a55a07c4fde78055095c43b31e8ba330fbd4e616f4a724a153d6969d484/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7061756c7a692f796969322d66696c652d6265686176696f722e737667)](https://packagist.org/packages/paulzi/yii2-file-behavior)[![Total Downloads](https://camo.githubusercontent.com/0912baa06f9958943b9b78999bfa10ef6fb937bb6664d85b1ef9efe687d6b288/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7061756c7a692f796969322d66696c652d6265686176696f722e737667)](https://packagist.org/packages/paulzi/yii2-file-behavior)

Features
--------

[](#features)

- single and multiple file store for ActiveRecord
- multiple image thumbs without extra db fields
- user-friendly API
- flexible class inheritance
- support yii alias and different location of web/real path

Install
-------

[](#install)

Install via Composer:

```
composer require paulzi/yii2-file-behavior:~1.2.0
```

or add

```
"paulzi/yii2-file-behavior" : "~1.2.0"
```

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

Usage
-----

[](#usage)

Use FileBehavior in model and fill attributes option:

```
class Sample extends \yii\db\ActiveRecord
{
    public function behaviors() {
        return [
            [
                'class' => 'paulzi\fileBehavior\FileBehavior',
                'path'  => '@webroot/files',
                'url'   => '@web/files',
                'attributes' => [
                    'file'  => [],
                    'files' => [
                        'class' => 'paulzi\fileBehavior\FileMultiple',
                    ],
                    'image' => [
                        'class' => 'paulzi\fileBehavior\Image',
                        'types' => [
                            'original' => [1200, 1200],
                            'mid'      => [400, 400],
                            'thm'      => [120, 120],
                        ],
                    ],
                    'images' => [
                        'class' => 'paulzi\fileBehavior\FileMultiple',
                        'item'  => [
                            'class' => 'paulzi\fileBehavior\Image',
                            'types' => [
                                'thm'  => [120, 120],
                            ],
                        ]
                    ],
                ],
            ],
        ];
    }
}
```

### Set files

[](#set-files)

```
$model = Sample::findOne(1);
$file  = UploadedFile::getInstance($model, 'file');
$model->file->value = $file->tempName;
$model->save();

$model = Sample::findOne(2);
$files = UploadedFile::getInstances($model, 'images');
foreach ($files as $file) {
    $model->images[] = $file->tempName;
}
$model->save();
```

### Get files

[](#get-files)

```
$model = Sample::findOne(1);
$url   = $model->file->url;
$path  = $model->file->path;

$model = Sample::findOne(2);
foreach ($model->images as $image) {
    echo $image->url;      // original image url
    echo $image->thm->url; // thm image url
}
```

### Remove files

[](#remove-files)

```
$model = Sample::findOne(1);
$model->file->value = null;
$model->save();

$model = Sample::findOne(1);
$model->files[2]->value = null;
$model->save();

$model = Sample::findOne(2);
$model->images->value = null;
$model->save();
```

### Image salt

[](#image-salt)

To generate a thumbnail file name is using a hash of the file name and type of thumbnail. If you need to protect the possibility of obtaining different types of thumbnail, set options salt by secret:

```
    public function behaviors() {
        return [
            [
                'class' => 'paulzi\fileBehavior\FileBehavior',
                'attributes' => [
                    'image' => [
                        'class' => 'paulzi\fileBehavior\Image',
                        'salt'  => 'secret',
                        'types' => [
                            'mid'      => [400, 400],
                            'thm'      => [120, 120],
                        ],
                    ],
                ],
            ],
        ];
    }
```

Set options globally
--------------------

[](#set-options-globally)

You can set salt, path and url options globally by using [Dependency Injection](http://www.yiiframework.com/doc-2.0/guide-concept-di-container.html):

`config\main.php`:

```
    'aliases' => [
        '@cdnWeb' => 'http://s.example.com',
    ],

    'on beforeRequest' => function () {
        \Yii::$container->set('paulzi\fileBehavior\FileBehavior', [
            'path' => '@cdn\web\files',
            'url'  => '@cdnWeb\web\files',
        ]);
        \Yii::$container->set('paulzi\fileBehavior\Image', [
            'salt' => Yii::$app->params['salt'],
        ]);
    },
```

`config\params-local.php`:

```
    'salt' => 'secret salt',
```

Extending
---------

[](#extending)

You can extend classes for change path building function or change file storing.

By default, files are stores in `{path}/{folder}/{12}/{12}/{1234567890abcdef1234567890ab}.{extension}`

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity67

Established project with proven stability

 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 ~87 days

Recently: every ~68 days

Total

19

Last Release

2179d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/9402208?v=4)[PaulZi](/maintainers/PaulZi)[@paulzi](https://github.com/paulzi)

---

Top Contributors

[![paulzi](https://avatars.githubusercontent.com/u/9402208?v=4)](https://github.com/paulzi "paulzi (22 commits)")

---

Tags

yii2file behaviorimage behavior

### Embed Badge

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

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

###  Alternatives

[liyunfang/yii2-upload-behavior

Upload behavior for Yii 2

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

PHPackages © 2026

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