Skip to content

codru/yii2-upload-file-behavior

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Upload File Behavior for Yii2

Behavior for simplifies file upload for Yii2.

Installation

Copy UploadFileBehavior.php to your app/components folder.

Usage

In view:

use yii\widgets\ActiveForm;

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>

<?= $form->field($model, 'picture')->fileInput() ?>

<div class="form-group">
    <?=
    Html::submitButton(
        $model->isNewRecord ? 'Create' : 'Update',
        ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']
    ) ?>
</div>

<?php ActiveForm::end(); ?>

In AR model:

use app\components\UploadFileBehavior;

public function behaviors()
{
    return [
        'file' => [
            'class' => UploadFileBehavior::className(),
            'attributeName' => 'picture',
            'savePath' => '@webroot/uploads',
            'generateNewName' => true,
            'protectOldValue' => true,
        ],
    ];
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published