yii2-upload-file Tools for upload and save file

  1. Requirements
  2. Usage
  3. Resources

Requirements

Yii 2.0 or above

Usage

See source for installation.

Attach behavior to model

public function behaviors()
{
    return [
        ...
        [
            'class' => 'mdm\upload\UploadBehavior',
            'attribute' => 'file', // required, use to receive input file
            'savedAttribute' => 'file_id', // optional, use to link model with saved file.
            'uploadPath' => '@common/upload', // saved directory. default to '@runtime/upload'
        ],
    ];
}

Modify your controller

public function actionCreate()
{
    ...
    if($model->load(Yii::$app->request->post()) && $model->validate()){
        if($model->saveUploadedFile() !== false){
            $model->save();
            // do something
        }
        return $this->redirect();
    }
    return $this->render('create',['model' => $model])
}

Resources

1 1
4 followers
0 downloads
Yii Version: 2.0
License: (not set)
Category: Others
Developed by: Misbahul D Munir
Created on: Oct 19, 2014
Last updated: 10 years ago

Related Extensions