S
S
skajtersen2017-05-25 13:59:02
Yii
skajtersen, 2017-05-25 13:59:02

How to pass filename from controller to model?

The controller is described as follows

if (Yii::$app->request->isPost) {
            $model->file = UploadedFile::getInstance($model, 'file');
            if ($model->file && $model->validate()) {
                $model->file->saveAs($model->file);

How to pass $model->file to some model method to insert filename into database?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-05-25
@webinar

$path = 'some/path/'
$model = new SomeModel();
$file = UploadedFile::getInstance($model, 'file');
if ($model->file && $model->validate()) {
$file->saveAs($path.$file->baseName);
$model->filename = $file->baseName;
$model->save();
}

www.yiiframework.com/doc-2.0/yii-web-uploadedfile.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question