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

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

maxodrom/yii2-upload-behavior
=============================

Upload behavior for Yii 2

0.2.0(8y ago)092BSD-3-ClausePHPPHP &gt;=5.6.0

Since Nov 23Pushed 5y ago1 watchersCompare

[ Source](https://github.com/maxodrom/yii2-upload-behavior)[ Packagist](https://packagist.org/packages/maxodrom/yii2-upload-behavior)[ Docs](https://github.com/mohorev/yii2-upload-behavior)[ RSS](/packages/maxodrom-yii2-upload-behavior/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (4)Versions (12)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/ddcb9054f8111c2459b37c25d3038548b05280fbdbe4f904b00ff56e6aa476ed/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6f686f7265762f796969322d75706c6f61642d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mohorev/yii2-upload-behavior)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/9289a4dd505b5e3e5e9c408a1374321285be2d19e4c3792a33109fc3471a8876/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6d6f686f7265762f796969322d75706c6f61642d6265686176696f722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/mohorev/yii2-upload-behavior)[![Quality Score](https://camo.githubusercontent.com/d6a7e9596bea2b9bdf0697328bef87407242f7735a7603008778a404b9372f85/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6d6f686f7265762f796969322d75706c6f61642d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/mohorev/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 maxodrom/yii2-upload-behavior "*"

```

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

```
"maxodrom/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:

```
