PHP code example of jaslin / yii2-upload-behavior

1. Go to this page and download the library: Download jaslin/yii2-upload-behavior library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jaslin / yii2-upload-behavior example snippets


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',
            ],
        ];
    }
}

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

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],
                ],
            ],
        ];
    }
}

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

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

php composer.phar