PHP code example of haohetao / yii2-upload-behavior

1. Go to this page and download the library: Download haohetao/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/ */

    

haohetao / yii2-upload-behavior example snippets


class Document extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['attachment', 'file','maxFiles' => 2, 'extensions' => 'doc, docx, pdf', 'on' => ['insert', 'update']],
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCategory()
    {
        return $this->hasOne(Category::class, [ 'id' => 'id_category' ]);
    }

    /**
     * @inheritdoc
     */
    function behaviors()
    {
        return [
            [
                'class' => UploadBehavior::class,
                'attributes' => [
                    [
                        'attribute' => 'attachment',
                        'path' => '@webroot/upload/docs/{category.id}',
                        'url' => '@web/upload/docs/{category.id}',
                        //'multiple' => true,
                        //'multipleSeparator' => '|',
                        //'nullValue' => '',
                        //'instanceByName' => false,
                        //'generateNewName' => true,//function($file){return uniqid().$file->name.;}
                        //'unlinkOnSave' => true,
                        //'deleteTempFile' => true,
                        //'scenarios' => ['insert', 'update'],
                    ],
                    [
                        ....
                    ]
                ],
                //'multipleSeparator' => '|',
                //'nullValue' => '',
                //'instanceByName' => false,
                //'generateNewName' => true,//function($file){return uniqid().$file->name.;}
                //'unlinkOnSave' => true,
                //'deleteTempFile' => true,
                'scenarios' => ['insert', 'update'],
            ],
        ];
    }
}

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

 $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 [
            [
                'class' => \liyunfang\file\UploadImageBehavior::class,
                'attributes' => [
                    [
                        'attribute' => 'image',
                        'path' => '@webroot/upload/user/{id}',
                        'url' => '@web/upload/user/{id}',
                        //'multiple' => true,
                        //'multipleSeparator' => '|',
                        //'nullValue' => '',
                        //'instanceByName' => false,
                        //'generateNewName' => true,//function($file){return uniqid().$file->name.;}
                        //'unlinkOnSave' => true,
                        //'deleteTempFile' => true,
                        //'scenarios' => ['insert', 'update'],
                        //'createThumbsOnSave' => true,
                        //'createThumbsOnRequest' => true,
                        // 'thumbs' => [
                        //    'thumb' => ['width' => 400, 'height' => 400,'quality' => 90],
                        //    'preview' => ['width' => 200, 'height' => 200],
                        //],
                        //'placeholder' => '@app/modules/user/assets/images/userpic.jpg',
                        //'thumbPath' => '@webroot/upload/user/{id}/thumb',
                        //'thumbUrl' => '@web/upload/user/{id}/thumb',
                    ],
                    [
                        ....
                    ]
                ],
                'scenarios' => ['insert', 'update'],
                //'multipleSeparator' => '|',
                //'nullValue' => '',
                //'instanceByName' => false,
                //'generateNewName' => true,//function($file){return uniqid().$file->name.;}
                //'unlinkOnSave' => true,
                //'deleteTempFile' => true,
                //'createThumbsOnSave' => true,
                //'createThumbsOnRequest' => false,
                // 'thumbs' => [
                //    'thumb' => ['width' => 400, 'height' => 400,'quality' => 90],
                //    'preview' => ['width' => 200, 'height' => 200],
                //],
            ],
        ];
    }
}

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

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

      $form->field($model, 'image')->widget(FileInput::class, [
          'options' => [
                'accept' => 'image/*',
                'multiple' => true,
                'name' => Html::getInputName($model, 'image').'[]',
      ],
      'pluginOptions' => [
          'initialPreview'=> !$model->image ? [] : $model->createPreview(['attribute' => 'image', 'profile' => 'preview']),
          'overwriteInitial'=> !$model->image ? false: true,