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

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

stesi/yii2-upload-behavior
==========================

Upload behavior for Yii 2

0.1.7.2(8y ago)029BSD 3-ClausePHPPHP &gt;=5.4.0

Since Nov 23Pushed 8y ago1 watchersCompare

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

READMEChangelog (2)Dependencies (4)Versions (11)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/d0beb6ab41af8ec9231bb3ec268eb119133f8abe441836770d8adde286e738d0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6f6e676f736f66742f796969322d75706c6f61642d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mongosoft/yii2-upload-behavior)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/1ae4c4bacd36ffcfc7c65bc1d8ba21e6b4d4868e8fee529cfe37b7b2e4383865/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6d6f6e676f736f66742f796969322d75706c6f61642d6265686176696f722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/mongosoft/yii2-upload-behavior)[![Coverage Status](https://camo.githubusercontent.com/a4d718f4dfd7dd5523ca15fb95560ae2504bf62061c182708c40a1f69ae334e7/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6d6f6e676f736f66742f796969322d75706c6f61642d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/mongosoft/yii2-upload-behavior/code-structure)[![Quality Score](https://camo.githubusercontent.com/756b65dae643c985fba45ea70ca5987460823b0a0586f4cf48ee41decf77f683/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6d6f6e676f736f66742f796969322d75706c6f61642d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/mongosoft/yii2-upload-behavior)[![Total Downloads](https://camo.githubusercontent.com/b8a7e77e28c962a8f8937b29b2449feef49326f7a720c1ed5dd835e50e0bae68/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6f6e676f736f66742f796969322d75706c6f61642d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mongosoft/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 mongosoft/yii2-upload-behavior "*"

```

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

```
"mongosoft/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::className(), [ 'id' => 'id_category' ]);
    }

    /**
     * @inheritdoc
     */
    function behaviors()
    {
        return [
            [
                'class' => UploadBehavior::className(),
                '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:

```
