PHP code example of mdmsoft / yii2-upload-file

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

    

mdmsoft / yii2-upload-file example snippets


return [
    ...
    'controllerMap' => [
        'file' => 'mdm\\upload\\FileController', // use to show or download file
    ],
];

public function behaviors()
{
	return [
        ...
		[
			'class' => 'mdm\upload\UploadBehavior',
			'attribute' => 'file', //  directory. default to '@runtime/upload'
            'autoSave' => true, // when true then uploaded file will be save before ActiveRecord::save()
            'autoDelete' => true, // when true then uploaded file will deleted before ActiveRecord::delete()
		],
	];
}

public function actionCreate()
{
    if($model->load(Yii::$app->request->post()) && $model->save()){
        ...
    }
    ...
}

public function actionCreate()
{
    if($model->load(Yii::$app->request->post()) && $model->validate()){
        if($model->saveUploadedFile() !== false){
            $model->save(false);
            ....
        }
        ...
    }
    ...
}

// in create or update view
<?= $form->field($model,'file')->fileInput(); 

public function actionCreate()
{
    ...
    if($model->load(Yii::$app->request->post()) && $model->validate()){
        $file = UploadedFile::getInstance($model, 'file');
        if($fileModel = FileModel::saveAs($file,['uploadPath' => '@common/upload'])){
            $model->fil_id = $fileModel->id;
            $model->save();
            ....
        }
        ...
    }
    
}

class MyModel extend ...
{
    public $file; // add this to your model class


php composer.phar 

php composer.phar 

"mdmsoft/yii2-upload-file": "~2.0"