How to load and update the image in Yii?

Hello! Please tell me how can I save image in Yii2? Announced additional variable $file and added the code to the model after saving. The data in the debugger is, but the root and base do not load...

Code download
public function beforeSave($insert)
{
 if ($file = UploadedFile::getInstance($this, 'file')){
 $dir = Yii::getAlias('@images').'/trener/';
 if (file_exists($dir.$this->img)){
unlink($dir.$this->img);
}
 if (file_exists($dir.'50x50/'.$this->img)){
unlink($dir.'50x50/'.$this->img);
}
 if (file_exists($dir.'/800x/'.$this->img)){
unlink($dir.'800x/'.$this->img);
}
 $this->img = strtotime('now').Yii::$app->getSecurity()->generateRandomString(6) . '.' .$file->extension;
$file->saveAs($dir.$this->img);

}
 return parent::beforeSave($insert); // TODO: Change the autogenerated stub
 }


The form field
<?= $form--->field($model, 'img')->widget(FileInput::classname(), [
 'pluginOptions' => [
 'showCaption' => false,
 'showRemove' => false,
 'showUpload' => false,
 'browseClass' => 'btn btn-primary btn-block',
 'browseIcon' => '<i class="glyphicon glyphicon-camera"></i> ',
 'browseLabel' => 'Choose image'
],
 'options' => ['accept' => 'image/*']
 ]) ?>


I would like to know the code to load the form when you update
June 14th 19 at 19:16
2 answers
June 14th 19 at 19:18
Solution
You added the variable file, and use img, but at the same time trying to save a picture from a file, which obviously is null, replace:
$form->field($model, 'img')
on
$form->field($model, 'file')
Well here is the stupidity:
$file->saveAs($dir.$this->img);
You have in $this->img no file name, there is null. So if you want to save with a name that was in the file, then probably this:
$name = $file->baseName . '.' . $file->extension; //the generated name and the extension
if($file->saveAs($dir.$name)){ //check whether it exists
 $this->img = $name; //if writable is preserved in the name of the attribute that will store the name in database
}
Yeah, that is pretty stupid) And I could not understand why this is so)) Thanks - jacquelyn_Nikolaus84 commented on June 14th 19 at 19:21
June 14th 19 at 19:20
Good night.
You a little not so use beforeSave();
public function beforeSave($insert)
{
if(parent::beforeSave($insert)){
 if ($file = UploadedFile::getInstance($this, 'file')){
 $dir = Yii::getAlias('@images').'/trener/';
 if (file_exists($dir.$this->img)){
unlink($dir.$this->img);
}
 if (file_exists($dir.'50x50/'.$this->img)){
unlink($dir.'50x50/'.$this->img);
}
 if (file_exists($dir.'/800x/'.$this->img)){
unlink($dir.'800x/'.$this->img);
}
 $this->img = strtotime('now').Yii::$app->getSecurity()->generateRandomString(6) . '.' .$file->extension;
$file->saveAs($dir.$this->img);
 } 
 return true;
}
 return false
 }


PS
Bourgeois
On Ridniy movi )))))
Good) I think that the problem is not in it. Something with the field img. It's always empty in the request.59d888f440d33863060354.png59d888fa05ad4129980971.png - jacquelyn_Nikolaus84 commented on June 14th 19 at 19:23
the file download in a separate model, as in the documentation. And check that the order might not pass validation. - jacquelyn_Nikolaus84 commented on June 14th 19 at 19:26

Find more questions by tags Yii