2017 © Pedro Peláez
 

yii2-extension yii2-upload-behavior

Upload behavior for Yii 2

image

jaslin/yii2-upload-behavior

Upload behavior for Yii 2

  • Sunday, January 28, 2018
  • by jas
  • Repository
  • 0 Watchers
  • 0 Stars
  • 144 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 49 Forks
  • 0 Open issues
  • 4 Versions
  • 3 % Grown

The README.md

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., (*1)

Latest Stable Version Total Downloads, (*2)

Installation

The preferred way to install this extension is through composer., (*3)

Either run, (*4)

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

or add, (*5)

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

to the require section of your composer.json file., (*6)

Usage

Upload file

Attach the behavior in your model:, (*7)

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:, (*8)

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
    <?= $form->field($model, 'image')->fileInput() ?>
    <div class="form-group">
        <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
    </div>
<?php ActiveForm::end(); ?>

Upload image and create thumbnails

Attach the behavior in your model:, (*9)

class User extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['image', 'image', 'extensions' => 'jpg, jpeg, gif, png', 'on' => ['insert', 'update']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'image' => [
                'class' => UploadImageBehavior::className(),
                'attribute' => 'image',
                'scenarios' => ['insert', 'update'],
                'placeholder' => '@app/modules/user/assets/images/userpic.jpg',
                'path' => '@webroot/upload/user/{id}',
                'url' => '@web/upload/user/{id}',
                'thumbs' => [
                    'thumb' => ['width' => 400, 'quality' => 90],
                    'preview' => ['width' => 200, 'height' => 200],
                ],
            ],
        ];
    }
}

Example view file:, (*10)

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
    <div class="form-group">
        <div class="row">
            <div class="col-lg-6">
                <!-- Original image -->
                <?= Html::img($model->getUploadUrl('image'), ['class' => 'img-thumbnail']) ?>
            </div>
            <div class="col-lg-4">
                <!-- Thumb 1 (thumb profile) -->
                <?= Html::img($model->getThumbUploadUrl('image'), ['class' => 'img-thumbnail']) ?>
            </div>
            <div class="col-lg-2">
                <!-- Thumb 2 (preview profile) -->
                <?= Html::img($model->getThumbUploadUrl('image', 'preview'), ['class' => 'img-thumbnail']) ?>
            </div>
        </div>
    </div>
    <?= $form->field($model, 'image')->fileInput(['accept' => 'image/*']) ?>
    <div class="form-group">
        <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
    </div>
<?php ActiveForm::end(); ?>

RESTfull

If you use UploadBehavior in RESTfull application and you do not need a prefix of the model name, set the property instanceByName = false:, (*11)

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

The Versions

28/01 2018

dev-master

9999999-dev https://github.com/JasLin/yii2-upload-behavior

Upload behavior for Yii 2

  Sources   Download

BSD-3-Clause BSD 3-Clause

The Requires

 

by Alexander Mohorev

file extension yii2 upload behavior image resize thumb

04/04 2015

0.1.2

0.1.2.0 https://github.com/mongosoft/yii2-upload-behavior

Upload behavior for Yii 2

  Sources   Download

BSD 3-Clause

The Requires

 

by Alexander Mohorev

file extension yii2 upload behavior image resize thumb

29/11 2014

0.1.1

0.1.1.0 https://github.com/mongosoft/yii2-upload-behavior

Upload behavior for Yii 2

  Sources   Download

BSD 3-Clause

The Requires

 

by Alexander Mohorev

file extension yii2 upload behavior image resize thumb

23/11 2014

0.1.0

0.1.0.0

Upload behavior for Yii 2

  Sources   Download

BSD 3-Clause

The Requires

 

by Alexander Mohorev

file yii2 upload behavior image resize thumb