I have a suggestion to upload a logo for companies in my application. Loading and saving when creating a profile work fine. But when updating, the logo remains blank if I do not download it again!
Here is my update form
<?php $form = ActiveForm::begin([ 'options' => ['enctype'=>'multipart/form-data'] ]); ?> ..... <?= $form->field($model, 'logo')->fileInput() ?> ...
My controller action
if ($model->load($_POST) ) { $file = \yii\web\UploadedFile::getInstance($model, 'logo'); if($file){ $model->logo=$file; } if($model->save()){ if($file) $file->saveAs(\Yii::$app->basePath . '/web/images/'.$file); } return $this->redirect(['profile']); } else { return $this->renderPartial('update', [ 'model' => $model, ]); }
My rules:
public function rules() { return [ [['logo'], 'image', 'extensions' => 'jpg,png', 'skipOnEmpty' => true], [['name'], 'required'], [['name', 'description'], 'string'], ]; }
Any ideas ????
source share