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

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

bajadev/yii2-upload-behavior
============================

Yii2 file/image upload behavior for ActiveRecord

3.0(3y ago)02.8k↓22.2%2MITPHPPHP &gt;=5.4.0

Since Feb 13Pushed 3y ago2 watchersCompare

[ Source](https://github.com/Bajadev/yii2-upload-behavior)[ Packagist](https://packagist.org/packages/bajadev/yii2-upload-behavior)[ Docs](http://bajadev.hu)[ RSS](/packages/bajadev-yii2-upload-behavior/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (2)Versions (12)Used By (0)

Yii2 file/image upload behavior for ActiveRecord
================================================

[](#yii2-fileimage-upload-behavior-for-activerecord)

This package is the set of two similar behaviors. The first one allows you to keep the uploaded file as-is. And the second one allows you to generate set of thumbnails for the uploaded image. Behaviors could be attached multiple times for different attributes.

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

[](#installation)

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

Either run

```
composer bajadev/yii2-upload-behavior "*"

```

or add

```
"bajadev/yii2-upload-behavior": "*"

```

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

FileUploadBehavior
------------------

[](#fileuploadbehavior)

This behavior allow you to add file uploading logic with ActiveRecord behavior.

### Usage

[](#usage)

Attach the behavior to your model class:

```
public function behaviors()
{
    return [
        [
            'class' => '\bajadev\upload\FileUploadBehavior',
            'attribute' => 'fileUpload',
            'filePath' => '@webroot/uploads/[[pk]].[[extension]]',
            'fileUrl' => '/uploads/[[pk]].[[extension]]',
        ],
    ];
}

```

You can use Yii path [aliases](http://www.yiiframework.com/doc-2.0/guide-concept-aliases.html)like `@app`, `@webroot`, `@web` in your path template configuration.

You can also use additional placeholders in path templates. Placeholder `[[foobar]]` will be replaced with appropriate placeholder value.

Possible path/url placeholders:

- `[[model]]` - model class name
- `[[pk]]` - value of the primary key
- `[[id]]` - the same as `[[pk]]`
- `[[attribute_name]]` - attribute value, for example `[[attribute_ownerId]]`
- `[[slug_attribute_name]]` - generate attribute value by Inflector, for example `[[attribute_ownerId]]`
- `[[md5_attribute_name]]` - generate md5 value, for example `[[attribute_ownerId]]`
- `[[id_path]]` - id subdirectories structure (if model primary key is `12345`, placeholder value will be `1/2/3/4/5`
- `[[basename]]` - original filename with extension
- `[[filename]]` - original filename without extension
- `[[extension]]` - original extension

Add validation rule:

```
public function rules()
{
    return [
        ['fileUpload', 'file'],
    ];
}

```

Setup proper form enctype:

```
$form = \yii\bootstrap\ActiveForm::begin([
    'enableClientValidation' => false,
    'options' => [
        'enctype' => 'multipart/form-data',
    ],
]);

```

File should be uploading fine.

You can get uploaded file url using model call:

```
echo $model->getUploadedFileUrl('fileUpload');

```

ImageUploadBehavior
-------------------

[](#imageuploadbehavior)

Image upload behavior extends file upload behavior with image thumbnails generation. You can configure set of different thumbnail profiles to generate.

### Usage

[](#usage-1)

Attach the behavior to your model class:

```
public function behaviors()
{
    return [
        [
             'class' => '\bajadev\upload\ImageUploadBehavior',
             'attribute' => 'imageUpload',
             'thumbs' => [
                 'thumb' => ['width' => 400, 'height' => 300, 'crop' => true, 'quality' => 80, 'inset' => true],
             ],
             'deleteOriginalFile' => true,
             'rotateImageByExif' => true,
             'filePath' => '@webroot/images/[[pk]].[[extension]]',
             'fileUrl' => '@web/images/[[pk]].[[extension]]',
             'thumbPath' => '@webroot/images/[[profile]]_[[pk]].[[extension]]',
             'thumbUrl' => '@web/images/[[profile]]_[[pk]].[[extension]]',
        ],
    ];
}

```

You can use Yii path [aliases](http://www.yiiframework.com/doc-2.0/guide-concept-aliases.html)like `@app`, `@webroot`, `@web` in your path template configuration.

You can also use additional placeholders in path templates. Placeholder `[[foobar]]` will be replaced with appropriate placeholder value.

Possible path/url placeholders:

- `[[model]]` - model class name
- `[[pk]]` - value of the primary key
- `[[id]]` - the same as `[[pk]]`
- `[[attribute_name]]` - attribute value, for example `[[attribute_ownerId]]`
- `[[slug_attribute_name]]` - generate attribute value by Inflector, for example `[[attribute_ownerId]]`
- `[[md5_attribute_name]]` - generate md5 value, for example `[[attribute_ownerId]]`
- `[[id_path]]` - id subdirectories structure (if model primary key is `12345`, placeholder value will be `1/2/3/4/5/`
- `[[basename]]` - original filename with extension
- `[[filename]]` - original filename without extension
- `[[extension]]` - original extension
- `[[profile]]` - thumbnail profile name, use it in thumbnail path/url

Add validation rule:

```
public function rules()
{
    return [
        ['imageUpload', 'file', 'extensions' => 'jpeg, gif, png'],
    ];
}

```

Setup proper form enctype:

```
$form = \yii\bootstrap\ActiveForm::begin([
    'options' => [
        'enctype' => 'multipart/form-data',
    ],
]);

```

File should be uploading fine.

You can get uploaded image url using model call:

```
echo $model->getImageFileUrl('imageUpload');

```

You can specify default image for models without uploaded image:

```
echo $model->getImageFileUrl('imageUpload', '/images/empty.jpg');

```

You can also get generated thumbnail image url:

```
echo $model->getThumbFileUrl('imageUpload', 'thumb');

```

You can specify default thumbnail image for models without uploaded image:

```
echo $model->getThumbFileUrl('imageUpload', 'thumb', '/images/thumb_empty.jpg');

```

Licence
-------

[](#licence)

MIT

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor2

2 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 ~278 days

Recently: every ~517 days

Total

11

Last Release

1319d ago

Major Versions

1.1.1 → 2.02017-01-24

2.1.2 → 3.02022-09-30

### Community

Maintainers

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

---

Top Contributors

[![metalagman](https://avatars.githubusercontent.com/u/1983796?v=4)](https://github.com/metalagman "metalagman (26 commits)")[![boliver20](https://avatars.githubusercontent.com/u/3207245?v=4)](https://github.com/boliver20 "boliver20 (14 commits)")[![RSol](https://avatars.githubusercontent.com/u/535813?v=4)](https://github.com/RSol "RSol (8 commits)")[![BioSin](https://avatars.githubusercontent.com/u/1442288?v=4)](https://github.com/BioSin "BioSin (5 commits)")[![ekzobrain](https://avatars.githubusercontent.com/u/2089893?v=4)](https://github.com/ekzobrain "ekzobrain (1 commits)")[![ElisDN](https://avatars.githubusercontent.com/u/1673552?v=4)](https://github.com/ElisDN "ElisDN (1 commits)")

---

Tags

imageresizefileuploadBehaviorthumb

### Embed Badge

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

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

###  Alternatives

[mohorev/yii2-upload-behavior

Upload behavior for Yii 2

128265.5k8](/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)
