Answer the question
In order to leave comments, you need to log in
How to properly upload images in Yii2?
Good afternoon!
I’m already building 3 sites on Yii, I’ve already successfully dug some of the functionality into components and don’t duplicate it, but the trouble is with the pictures.
Now I do this:
public function actionUpdate($id)
{
$model = $this->findModel($id);
$currentData = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$image = UploadedFile::getInstance($model, 'image_name');
if ($image != null) {
if ($currentData->image_name)
ImagesController::deleteImage($currentData->image_name);
$model->image_name = ImagesController::saveImage($image);
} else {
$model->image_name = $currentData->image_name;
}
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question