PHPackages                             baha2odeh/yii2-easy-fileupload-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. baha2odeh/yii2-easy-fileupload-behavior

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

baha2odeh/yii2-easy-fileupload-behavior
=======================================

Yii2 Easy File Upload Behavior

v1.0(7y ago)11.8kApache-2.0PHP

Since Apr 4Pushed 7y ago1 watchersCompare

[ Source](https://github.com/Baha2Odeh/yii2-easy-fileupload-behavior)[ Packagist](https://packagist.org/packages/baha2odeh/yii2-easy-fileupload-behavior)[ RSS](/packages/baha2odeh-yii2-easy-fileupload-behavior/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Yii2 Easy File Upload Behavior
==============================

[](#yii2-easy-file-upload-behavior)

Yii2 Easy File 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 baha2odeh/yii2-easy-fileupload-behavior "*"

```

or add

```
"baha2odeh/yii2-easy-fileupload-behavior": "*"

```

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

you have to not use UploadedFile::getInstance anymore this can be done by this behavior

Usage
-----

[](#usage)

Once the extension is installed, simply use it in your code by : add this on your php model

```
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['images_files','image_file'],'file'], //@TODO add your rules here
        ];
    }

    public function behaviors()
    {
       return [
           [
               'class' => EasyFileUploadBehavior::className(),
               'relations' => [
                   'images_files' => 'images', // images means your HasMany Relation Name
                   'image_file' => 'image', // image means your HasOne Relation Name
               ],
               'uploadCallBack' => function($relationName,UploadedFile $file){
               	   // do your magic here and return one model that you save the image on it
               	   // if return is null file will skipped
               	   $file->saveAs('upload-path/'.$file->name);
                   $image = new Image();
                   $image->filename = $file->name;
                   $image->save(false);
                   return $image;
               }
           ]
       ];
    }

    //// demo only ////

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getImage()
    {
        return $this->hasOne(Image::className(), ['image_id' => 'image_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getArticleImages()
    {
        return $this->hasMany(ArticleImage::className(), ['article_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getImages()
    {
        return $this->hasMany(Image::className(), ['image_id' => 'image_id'])->via('articleImages');
    }
    //// demo only ////
```

then use it on your form like this

```
