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

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

pafnow/yii2-upload-behavior
===========================

Yii2 file/image upload behavior

026PHP

Since Nov 11Pushed 11y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

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

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

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

[](#fileuploadbehavior)

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

### Usage

[](#usage)

Attach the behavior to your model class:

```
public function behaviors()
{
    return [
        'file-upload' => [
            'class' => '\lagman\upload\FileUploadBehavior',
            'attribute' => 'fileUpload',
            'filePath' => '[[web_root]]/uploads/[[id]].[[extension]]',
            'fileUrl' => '/uploads/[[id]].[[extension]]',
        ],
    ];
}

```

Possible path/url placeholders:

- \[\[app\_root\]\] - application root
- \[\[web\_root\]\] - web root
- \[\[model\]\] - model name
- \[\[id\]\] - model id
- \[\[id\_path\]\] - id subdirectories structure
- \[\[parent\_id\]\] - parent object primary key value
- \[\[basename\]\] - original filename with extension
- \[\[filename\]\] - original filename without extension
- \[\[extension\]\] - original extension
- \[\[base\_url\]\] - site base url

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 [
        'image-upload' => [
             'class' => '\lagman\upload\ImageUploadBehavior',
             'attribute' => 'imageUpload',
             'thumbs' => [
                 'thumb' => ['width' => 400, 'height' => 300],
             ],
             'filePath' => '[[web_root]]/images/[[model]]/[[id]].[[extension]]',
             'fileUrl' => '/images/[[model]]/[[id]].[[extension]]',
             'thumbPath' => '[[web_root]]/images/[[model]]/[[profile]]_[[id]].[[extension]]',
             'thumbUrl' => '/images/[[model]]/[[profile]]_[[id]].[[extension]]',
        ],
    ];
}

```

Possible path/url placeholders:

- \[\[app\_root\]\] - application root
- \[\[web\_root\]\] - web root
- \[\[model\]\] - model name
- \[\[id\]\] - model id
- \[\[id\_path\]\] - id subdirectories structure
- \[\[basename\]\] - original filename with extension
- \[\[filename\]\] - original filename without extension
- \[\[extension\]\] - original extension
- \[\[base\_url\]\] - site base url
- \[\[profile\]\] - thumbnail profile name

Add validation rule:

```
public function rules()
{
    return [
        ['imageUpload', 'file', 'types' => 'jpeg, 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->getUploadedFileUrl('imageUpload');

```

You can also get generated thumbnail image url:

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

```

Licence
-------

[](#licence)

MIT

Contacts
--------

[](#contacts)

-
-

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

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://www.gravatar.com/avatar/1a9ba98d9321bd5a190df864de62767fdabeeb51d0485b42f1473aa7b5828714?d=identicon)[pafnow](/maintainers/pafnow)

---

Top Contributors

[![pafnow](https://avatars.githubusercontent.com/u/8747037?v=4)](https://github.com/pafnow "pafnow (3 commits)")

---

Tags

yii2

### Embed Badge

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

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

###  Alternatives

[knplabs/gaufrette

PHP library that provides a filesystem abstraction layer

2.5k39.8M123](/packages/knplabs-gaufrette)[google/cloud-storage

Cloud Storage Client for PHP

34390.8M125](/packages/google-cloud-storage)[illuminate/filesystem

The Illuminate Filesystem package.

15261.6M2.6k](/packages/illuminate-filesystem)[superbalist/flysystem-google-storage

Flysystem adapter for Google Cloud Storage

26320.6M30](/packages/superbalist-flysystem-google-storage)[creocoder/yii2-flysystem

The flysystem extension for the Yii framework

2931.7M62](/packages/creocoder-yii2-flysystem)[flowjs/flow-php-server

PHP library for handling chunk uploads. Works with flow.js html5 file uploads.

2451.6M15](/packages/flowjs-flow-php-server)

PHPackages © 2026

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