Yii 2.0: yii2-upload-file

Tools for upload and save file
4 followers

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

Be the first person to leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet