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

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

ptheofan/yii2-file-upload-behavior
==================================

A highly customisable system to make file-uploads management as trivial as it should be.

v1.0.11(4y ago)049BSD-3-ClausePHPPHP &gt;=7.4.0

Since Dec 30Pushed 4y ago1 watchersCompare

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

READMEChangelogDependencies (3)Versions (13)Used By (0)

Documentation is incomplete and will add more info and examples as time allows. Feel free to open an issue if there's something you want to ask or add to the documentation. Currently it comes with a facade for the FlySystem only. In due time I will add also a facade for basic local filesystem

The entire system is designed to be very customisable and extensible. There are a couple of points that can be (and will be) improved to make it as extensible and customisable as possible whilst keeping it sane.

This is an example of how to configure the behavior and use it to allow users to upload their avatar. In this example we want to achieve the following

1. access the property as `avatar`
    1. see `modelVirtualAttribute`
2. store the filename to the database in the column `avatar_hash`.
    1. see `modelAttribute`
3. Filename prefixed with row ID, filename the SHA1 of the file and suffix the version
    1. For this we set the `filenameGenerator` to use the `CallbackFilenameGenerator`
    2. Configure it to not include the extension (versions will take care of .ext in this case)
    3.
4. Keep the following versions
    1. Unresized upload in PNG format `PngBaseVersion`. Final name will look something like 512-deadbeef.png
    2. sm (width = 64 pixels) with suffix `-sm`. Final name will look something like 512-deadbeef-sm.png
    3. md (width = 256 pixels) with suffix `-md`. Final name will look something like 512-deadbeef-md.png
    4. lg (width = 512 pixels) with suffix `-lg`. Final name will look something like 512-deadbeef-lg.png

When the request arrives simply push the `UploadedFile` instance to the `avatar` model property, save the model and voila, image stored as per the provided configuration and all versions are generated.

```
$model->avatar = UploadedFile::getInstance($model, 'file');
$model->save();
```

When you want to retrieve a particular version of the uploaded file simply call

```
$url = $model->avatar->getVersion('sm')->getUrl();
```

You can also print the object to get detailed helpful information

```
echo $model->avatar;
```

In the following example we configure our model to use the database column `avatar_hash` to store the file information in the database and set virtual attribute to avatar. This means

1. `$model->avatar_hash` contains the value produced by the generator (you typically don't care to touch this column).
2. `$model->avatar` is your accessor to the image. Calling `$model->avatar->getVersion('sm')->getUrl()` will return the complete URL to the small version of the file.
3. You can assign an uploaded file as simple as `$model->avatar = UploadedFile::getInstance($model, 'avatar')`
4. You can push a base64 encoded image as simply as `$model->avatar = $myBase64EncodedImage`
5. You can push a binary string simply by `$model->avatar = file_get_contents('my_file.png')`

```
public function behaviors(): array
    {
        return [
            'avatar' => [
                'class' => FileAttribute::class,
                'modelAttribute' => 'avatar_hash',
                'modelVirtualAttribute' => 'avatar',
                'generateAfterInsert' => true,
                'filenameGenerator' => [
                    'class' => CallbackFilenameGenerator::class,
                    'withExt' => false,
                    'callback' => static function(string $file, ActiveRecord $model, IFileAttribute $attr) {
                        return sprintf('%s-%s', $model->id, sha1_file($file));
                    }
                ],
                'versions' => [
                    [
                        // will produce
                        'class' => PngBaseVersion::class,
                        'name' => 'sm',
                        'basePath' => '/avatars',
                        'baseUrl' => 'https://cdn.example.com/avatars',
                        'width' => 64,
                        'suffix' => '-sm',
                    ],
                    [
                        'class' => PngResizedVersion::class,
                        'name' => 'sm',
                        'basePath' => '/avatars',
                        'baseUrl' => 'https://cdn.example.com/avatars',
                        'width' => 64,
                        'suffix' => '-sm',
                    ],
                    [
                        'class' => PngResizedVersion::class,
                        'name' => 'md',
                        'basePath' => '/avatars',
                        'baseUrl' => 'https://cdn.example.com/avatars',
                        'width' => 256,
                        'suffix' => '-md',
                    ],
                    [
                        'class' => PngResizedVersion::class,
                        'name' => 'lg',
                        'basePath' => '/avatars',
                        'baseUrl' => 'https://cdn.example.com/avatars',
                        'width' => 512,
                        'suffix' => '-lg',
                    ],
                ],
            ],
        ];
    }
    ```
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

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

Recently: every ~97 days

Total

12

Last Release

1564d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/880939?v=4)[Paris](/maintainers/ptheofan)[@ptheofan](https://github.com/ptheofan)

---

Top Contributors

[![ptheofan](https://avatars.githubusercontent.com/u/880939?v=4)](https://github.com/ptheofan "ptheofan (21 commits)")

### Embed Badge

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

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

###  Alternatives

[mihaildev/yii2-elfinder

Yii2 ElFinder

169658.8k52](/packages/mihaildev-yii2-elfinder)[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)
