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

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

bscheshirwork/yii2-file-upload-behavior
=======================================

Tool set for upload file and store to the model-depend folder

036PHP

Since May 6Pushed 7y ago1 watchersCompare

[ Source](https://github.com/bscheshirwork/yii2-file-upload-behavior)[ Packagist](https://packagist.org/packages/bscheshirwork/yii2-file-upload-behavior)[ RSS](/packages/bscheshirwork-yii2-file-upload-behavior/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

Add upload file to form and store it to model-id-depends folder
===============================================================

[](#add-upload-file-to-form-and-store-it-to-model-id-depends-folder)

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

[](#installation)

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

add

```
"bscheshirwork/yii2-file-upload-behavior": "@dev"

```

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

The namespace of module is a `bscheshirwork\fub`

Usage
-----

[](#usage)

We can add of this elements in some places for complex solution:

In form (also can be combine with model. In this case add `fileUpload` and `fileSave` to model. See example below)

TagForm.php

```
use bscheshirwork\fub\FileUploadBehavior;
...
    public function behaviors()
    {
        return [
            'fileUpload' => [ // name is important
                'class' => FileUploadBehavior::class,
                'attribute' => 'image',
                'tempDirectory' => '@storageWeb/images/temp',
                'tempDirectoryUrl' => '@storageUrl/images/temp',
            ],
        ];
    }

    public function form2Models(): ActiveRecordInterface
    {
        $this->getBehavior('fileUpload')->uploadFile();
        $this->_model->getBehavior('fileSave')->uploader = $this->getBehavior('fileUpload');

        return $this->_model;
    }
```

In model

Tag.php

```
use bscheshirwork\fub\FileSaveBehavior;
...
    public function behaviors()
    {
        return [
            'fileSave' => [ // name is important
                'class' => FileSaveBehavior::class,
                'directory' => '@storageWeb/images/single',
                'directoryUrl' => '@storageUrl/images/single',
                'type' => 'tag',
                'fileVersions' => [
                    'default' => [
                        'fileName' => [
                            /** @see FileSaveBehavior::defaultFileName() */
                            [FileSaveBehavior::class, 'defaultFileName'],
                            'extension' => 'svg',
                        ],
                        'fileUrl' => [
                            /** @see FileSaveBehavior::defaultFileUrl() */
                            [FileSaveBehavior::class, 'defaultFileUrl'],
                            'extension' => 'svg',
                            'fileNameVersion' => 'default',
                        ],
                        'postProcessing' => [
                            /** @see FileSaveBehavior::defaultAttachFileToModel() */
                            [FileSaveBehavior::class, 'defaultAttachFileToModel'],
                            'fileNameVersion' => 'default',
                        ],
                    ],
                ],
            ],
        ];
    }
```

In view

\_form.php

```
use bscheshirwork\fub\FileInputWidget;
?>
...
