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

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

haohetao/yii2-upload-behavior
=============================

Upload behavior for Yii 2

1.2.0(6y ago)1797↓100%1BSD-3-ClausePHPPHP &gt;=7.1.0

Since Oct 20Pushed 5y ago1 watchersCompare

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

READMEChangelog (4)Dependencies (2)Versions (7)Used By (0)

Upload behavior for Yii 2 [中文](https://github.com/haohetao/yii2-upload-behavior/blob/master/README-ZH-CN.md)
============================================================================================================

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

Note: Reference project

This behavior automatically uploads file and fills the specified attribute with a value of the name of the uploaded file.

[![Effect picture 1](https://github.com/phpyii/wr/raw/master/images/UploadBehavior1.png "Effect picture 1")](https://github.com/phpyii/wr/blob/master/images/UploadBehavior1.png)

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

[](#installation)

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

Either run

```
composer require --prefer-dist liyunfang/yii2-upload-behavior "*"

```

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

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

Usage
-----

[](#usage)

### Upload file

[](#upload-file)

Attach the behavior in your model:

```
class Document extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['attachment', 'file','maxFiles' => 2, '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,
                'attributes' => [
                    [
                        'attribute' => 'attachment',
                        'path' => '@webroot/upload/docs/{category.id}',
                        'url' => '@web/upload/docs/{category.id}',
                        //'multiple' => true,
                        //'multipleSeparator' => '|',
                        //'nullValue' => '',
                        //'instanceByName' => false,
                        //'generateNewName' => true,//function($file){return uniqid().$file->name.;}
                        //'unlinkOnSave' => true,
                        //'deleteTempFile' => true,
                        //'scenarios' => ['insert', 'update'],
                    ],
                    [
                        ....
                    ]
                ],
                //'multipleSeparator' => '|',
                //'nullValue' => '',
                //'instanceByName' => false,
                //'generateNewName' => true,//function($file){return uniqid().$file->name.;}
                //'unlinkOnSave' => true,
                //'deleteTempFile' => true,
                'scenarios' => ['insert', 'update'],
            ],
        ];
    }
}
```

Example view single file:

```
