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

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

h0rseduck/yii2-upload-behavior
==============================

Upload behavior for Yii 2

0.3.5(5y ago)0146BSD-3-ClausePHPPHP &gt;=5.6.0

Since Nov 23Pushed 5y ago1 watchersCompare

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

READMEChangelogDependencies (4)Versions (19)Used By (0)

Upload behavior for Yii 2
=========================

[](#upload-behavior-for-yii-2)

This behavior automatically uploads file and fills the specified attribute with a value of the name of the uploaded file. This code is inspired by, but not derived from, .

[![Latest Version](https://camo.githubusercontent.com/abe47534ec8bf02d9b7e7cb75dd75934d9da8cc3f554d9ded93e2403a59b093e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68307273656475636b2f796969322d75706c6f61642d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/h0rseduck/yii2-upload-behavior)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/cc797052d99ce22b2f14340e03769faf8ab40ad7dc5dbb9b5365051fdc9681ab/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f68307273656475636b2f796969322d75706c6f61642d6265686176696f722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/h0rseduck/yii2-upload-behavior)[![Quality Score](https://camo.githubusercontent.com/dceafdcb8d3f3a7f7ec6869b9f653b501685c036e88ab1502ff4cb1dd46809f1/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f68307273656475636b2f796969322d75706c6f61642d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/h0rseduck/yii2-upload-behavior)

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

[](#installation)

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

Either run

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

```

or add this code line to the `require` section of your `composer.json` file:

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

Usage
-----

[](#usage)

### Upload file

[](#upload-file)

Attach the behavior in your model:

```
class Document extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['file', 'file', 'extensions' => 'doc, docx, pdf', 'on' => ['insert', 'update']],
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCategory()
    {
        return $this->hasOne(Category::class, [ 'id' => 'id_category' ]);
    }

    /**
     * @inheritdoc
     */
    function behaviors()
    {
        return [
            [
                'class' => UploadBehavior::class,
                'attribute' => 'file',
                'scenarios' => ['insert', 'update'],
                'path' => '@webroot/upload/docs/{category.id}',
                'url' => '@web/upload/docs/{category.id}',
            ],
        ];
    }
}
```

Set model scenario in controller action:

```
class Controller extends Controller
{
    public function actionCreate($id)
    {
        $model = $this->findModel($id);
        $model->setScenario('insert'); // Note! Set upload behavior scenario.

        ...
        ...
    }
}
```

Example view file:

```
