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

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

jaslin/yii2-upload-behavior
===========================

Upload behavior for Yii 2

0.1.2(11y ago)0163BSD 3-ClausePHPPHP &gt;=5.4.0

Since Nov 23Pushed 8y agoCompare

[ Source](https://github.com/JasLin/yii2-upload-behavior)[ Packagist](https://packagist.org/packages/jaslin/yii2-upload-behavior)[ Docs](https://github.com/mongosoft/yii2-upload-behavior)[ RSS](/packages/jaslin-yii2-upload-behavior/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (4)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.

[![Latest Stable Version](https://camo.githubusercontent.com/e8d85665b462d80e96065a0b915cc17a51f178b6c5fca191dc15e508f34c7391/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6e676f736f66742f796969322d75706c6f61642d6265686176696f722f762f737461626c652e706e67)](https://packagist.org/packages/mongosoft/yii2-upload-behavior)[![Total Downloads](https://camo.githubusercontent.com/222f8a13a7ff2c4b8fa4642873545601eb20091d9027e4916ab1e3fe49db8101/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6e676f736f66742f796969322d75706c6f61642d6265686176696f722f646f776e6c6f6164732e706e67)](https://packagist.org/packages/mongosoft/yii2-upload-behavior)

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

[](#installation)

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

Either run

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

```

or add

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

to the `require` section of your `composer.json` file.

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']],
        ];
    }

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

Example view file:

```
