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

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

valiant/yii2-upload-behavior
============================

Yii2 file/image upload behavior for ActiveRecord

1.1.3(9y ago)0139MITPHPPHP &gt;=5.4.0

Since Jun 3Pushed 9y ago1 watchersCompare

[ Source](https://github.com/one-more-developer/yii2-upload-behavior)[ Packagist](https://packagist.org/packages/valiant/yii2-upload-behavior)[ Docs](https://github.com/one-more-developer/yii2-upload-behavior)[ RSS](/packages/valiant-yii2-upload-behavior/feed)WikiDiscussions master Synced 1mo ago

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

Description
===========

[](#description)

This is fork for yii-dream-team/yii2-upload-behavior from Yii Dream Team ()

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

```
php composer.phar require --prefer-dist valiant/yii2-upload-behavior "*"

```

or add

```
"valiant/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' => '\valiant\behaviors\FileUploadBehavior',
            'attribute' => 'attachment',
            '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]]`
- `[[extension]]` - original extension
- `[[attribute]]` - attribute name
- `[[id_path]]` - id subdirectories structure (if model primary key is `12345`, placeholder value will be `1/2/3/4/5/0/0/0/0/0`)
- `[[id_hash]]` - id subdirectories structure by md5 hash (if model primary key is `12345`, md5 hash is `827ccb0eea8a706c4c34a16891f84e7b` placeholder value will be `8/27/ccb0eea8a706c4c34a16891f84e7b`)
- `[[id_hash_piece]]` - id hash dex piece, will be in range 0-255 (if model primary key is `12345`, md5 hash is `827ccb0eea8a706c4c34a16891f84e7b`, first 2 hex numbers is `82`, convert hexDex and placeholder value will be `130`)
- `[[attribute_name]]` - attribute value, for example `[[attribute_ownerId]]`

Add validation rule:

```
public function rules()
{
    return [
        ['attachment', '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('attachment');

```

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' => '\valiant\behaviors\ImageUploadBehavior',
             'attribute' => 'image',
             'thumbs' => [
                 'thumb' => ['width' => 400, 'height' => 300],
                 'albumThumb' => ['width' => 200],
                 'albumLogo' => ['height' => 500],
             ],
             'filePath' => '@webroot/images/[[pk]].[[extension]]',
             'fileUrl' => '/images/[[pk]].[[extension]]',
             'thumbPath' => '@webroot/images/[[profile]]_[[pk]].[[extension]]',
             'thumbUrl' => '/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]]`
- `[[extension]]` - original extension
- `[[attribute]]` - attribute name
- `[[id_path]]` - id subdirectories structure (if model primary key is `12345`, placeholder value will be `1/2/3/4/5/0/0/0/0/0`)
- `[[id_hash]]` - id subdirectories structure by md5 hash (if model primary key is `12345`, md5 hash is `827ccb0eea8a706c4c34a16891f84e7b` placeholder value will be `8/27/ccb0eea8a706c4c34a16891f84e7b`)
- `[[id_hash_piece]]` - id hash dex piece, will be in range 0-255 (if model primary key is `12345`, md5 hash is `827ccb0eea8a706c4c34a16891f84e7b`, first 2 hex numbers is `82`, convert hexDex and placeholder value will be `130`)
- `[[attribute_name]]` - attribute value, for example `[[attribute_ownerId]]`
- `[[profile]]` - thumbnail profile name, use it in thumbnail path/url
- `[[width]]` - thumbnail width, use it in thumbnail path/url
- `[[height]]` - thumbnail height, use it in thumbnail path/url

Add validation rule:

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

```

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 image url using model call:

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

```

You can specify default image for models without uploaded image:

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

```

You can also get generated thumbnail image url:

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

```

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

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

```

Licence
-------

[](#licence)

MIT

Links
-----

[](#links)

- [Source code on GitHub](https://github.com/one-more-developer/yii2-upload-behavior)
- [Composer package on Packagist](https://packagist.org/packages/valiant/yii2-upload-behavior)

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 60% 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 ~182 days

Total

4

Last Release

3456d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cb5fcff6dfac91212bf16dd0ffc904b5f3de9bcf98341f3456c28cf3368ce6b5?d=identicon)[hunter-kaan](/maintainers/hunter-kaan)

---

Top Contributors

[![metalagman](https://avatars.githubusercontent.com/u/1983796?v=4)](https://github.com/metalagman "metalagman (24 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 (4 commits)")[![hunter-kaan](https://avatars.githubusercontent.com/u/5260983?v=4)](https://github.com/hunter-kaan "hunter-kaan (4 commits)")

---

Tags

thumbnailimageresizefileuploadBehavior

### Embed Badge

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

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

###  Alternatives

[yii-dream-team/yii2-upload-behavior

Yii2 file/image upload behavior for ActiveRecord

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

Upload behavior for Yii 2

128265.5k9](/packages/mohorev-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)

PHPackages © 2026

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