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

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

tpmanc/yii2-file-behavior
=========================

Yii2 file uploading

1.2.5(4y ago)23551BSD-3-ClausePHPPHP &gt;=5.4.0

Since Aug 3Pushed 4y ago1 watchersCompare

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

READMEChangelog (8)Dependencies (1)Versions (9)Used By (0)

yii2-file-behavior
==================

[](#yii2-file-behavior)

Yii 2 file uploading

Install via Composer
--------------------

[](#install-via-composer)

Run the following command

```
$ composer require tpmanc/yii2-file-behavior "*"
```

or add

```
$ "tpmanc/yii2-file-behavior": "*"
```

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

Migrations
----------

[](#migrations)

Create migration by following command

```
$ yii migrate/create images
```

Open the `/path/to/migrations/m_xxxxxx_xxxxxx_images.php` file and add following code to `up()` method

```
        $this->createTable('image', [
            'id' => Schema::TYPE_PK,
            'itemId' => Schema::TYPE_INTEGER . ' NOT NULL',
            'order' => Schema::TYPE_INTEGER . ' NOT NULL',
            'extension' => Schema::TYPE_STRING . '(10) NOT NULL',
            'hash' => Schema::TYPE_STRING . '(32) NOT NULL',
        ]);

        $this->createTable('imageSize', [
            'id' => Schema::TYPE_PK,
            'imageId' => Schema::TYPE_INTEGER . ' NOT NULL',
            'path' => Schema::TYPE_STRING . '(255) NOT NULL',
            'size' => Schema::TYPE_STRING . '(255) NOT NULL',
        ]);
```

Create model
------------

[](#create-model)

Generate Active Record model for new `image` and `imageSize` tables

Configuring
-----------

[](#configuring)

Attach the behavior to your model class:

```
use tpmanc\filebehavior\ImageBehavior;

\\ ...

    public $file;

    public function behaviors()
    {
        return [
            'ImageBehavior' => [
                'class' => ImageBehavior::className(),
                'imageModel' => 'models\Image',
                'imageSizeModel' => 'models\ImageSize',
                'imageVariable' => 'file',
                'imageFolder' => '@upload',
                'webImageFolder' => '@webupload',
                'noImagePath' => '@webupload/no-image.png',
            ],
        ];
    }

    public function rules()
    {
        ['file', 'file', 'extensions' => ['png', 'jpg'], 'maxSize' => 1024*1024*1024, 'maxFiles' => 4],
    }
```

If file hash will be like "6e3c797abee0ff2803ef1f952f187d2f" the file will be located in `@upload/images/6e/3c/{id from image table}.jpg`

To save several sizes of image add:

```
    public $file;

    public function behaviors()
    {
        return [
            'ImageBehavior' => [
                'class' => ImageBehavior::className(),
                'fileModel' => 'models\Image',
                'fileVar' => 'file',
                'fileFolder' => '@upload/images',
                'imageSizes' => [
                    'original' => [
                        'folder' => 'original',
                    ],
                    'big' => [
                        'width' => 800,
                        'height' => 600,
                        'folder' => 'big',
                    ],
                    'small' => [
                        'width' => 64,
                        'height' => 64,
                        'folder' => 'small',
                    ],
                ],
            ],
        ];
    }

    public function rules()
    {
        ['file', 'file', 'extensions' => ['png', 'jpg'], 'maxSize' => 1024*1024*1024, 'maxFiles' => 1],
    }
```

If file hash will be like "6e3c797abee0ff2803ef1f952f187d2f" - result 3 images:

- `@upload/images/original/6e/3c/{id from image table}.jpg`
- `@upload/images/big/6e/3c/{id from image table}.jpg`
- `@upload/images/small/6e/3c/{id from image table}.jpg`

View file
---------

[](#view-file)

Example of view file

```
 ['enctype' => 'multipart/form-data']]) ?>
    field($model, 'file[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>

        isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

```

Geting images
-------------

[](#geting-images)

Get single image:

```
