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

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

xisio/yii2-upload-behavior
==========================

Improved version Upload behavior for Yii 2 by mohorev/yii2-upload-behavior

v1.1(6y ago)03BSD-3-ClausePHPPHP &gt;=5.6.0

Since Nov 23Pushed 6y agoCompare

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

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

[![Build Status](https://camo.githubusercontent.com/e09831e0b50dfab9b3637f282299a5c5c792cdcc9fcce47bcf94b1a731ba8b5f/68747470733a2f2f6170692e7472617669732d63692e6f72672f73687572696b326b352f796969322d75706c6f61642d6265686176696f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/shurik2k5/yii2-upload-behavior)[![Total Downloads](https://camo.githubusercontent.com/66c8bc74c7384312e65125069c5c3dab100cb9be522e82c5f1ed82f88feec1f6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73687572696b326b352f796969322d75706c6f61642d6265686176696f722e737667)](https://packagist.org/packages/shurik2k5/yii2-upload-behavior)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/fad1128ff8f3522315665b1f59b4ce60142cc761ec11c8668a51b9b78195be16/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f73687572696b326b352f796969322d75706c6f61642d6265686176696f722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/shurik2k5/yii2-upload-behavior/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/d6d34e1929b96a1e928a1caa069336b3766e92a333d8cb2ccacd083a43f6d4a4/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f73687572696b326b352f796969322d75706c6f61642d6265686176696f722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/shurik2k5/yii2-upload-behavior/?branch=master)

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.

In this behaviour added ability to load file from URL and local files, and attach behavior for two or more attributes.

This is an enhanced version of

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

[](#installation)

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

Either run

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

```

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

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

Usage
-----

[](#usage)

### Upload file from input forms

[](#upload-file-from-input-forms)

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' => \mohorev\file\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:

```
