While uploading multiple file getting this error:
When I put [['file'], 'file', 'maxFiles' => 4],
in model getting following error:
Call to a member function saveAs() on null
But when I put this [['file'], 'file'],
in model, its uploading.
Why am I getting error?
View:
<?php echo $form->field($model,'file[]')->label(false)->widget(FileInput::classname(),
[
'options'=>['accept'=>'image/*', 'multiple'=>true],
'pluginOptions'=>['allowedFileExtensions'=>['jpg','gif','png']
]]);
?>
Controller:
public function actionCreate()
{
$model = new RoomTypes();
if ($model->load(Yii::$app->request->post()))
{
$imageName = $model->room_type;
$model->file = UploadedFile::getInstance($model, 'file');
$model->file->saveAs( 'uploads/room_img/'.$imageName.'.'.$model->file->extension);
//save the path in the db column
$model->images = 'uploads/room_img/'.$imageName.'.'.$model->file->extension;
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
}
else
{
return $this->render('create', [
'model' => $model,
]);
}
}