Answer the question
In order to leave comments, you need to log in
How to upload and delete single image in YII2?
Hi all. Please tell me how to implement the simplest implementation of attaching images to articles in YII 2.
Oddly enough, I went through a bunch of lessons and in all the same trouble, attaching a file is all OK, for example, here is such a simple controller:
$model->file = UploadedFile::getInstance($model, 'file');
$model->file->saveAs(‘uploads/blog_'.$imageName.'.'.$model->file->extension);
$model->logo = 'uploads/blog_'.$imageName.'.'.$model->file->extension;
$model->save();
Answer the question
In order to leave comments, you need to log in
Вот мое простое поведение для аплоада картинки, в событии after_delete происходит проверка наличия файла, и его удаление.
https://github.com/mhthnz/yii2-helpful-behaviors/b...
public function deleteImage($event)
{
$imagePath = Yii::getAlias($this->_uploadPath . '/' . $this->_oldImage);
if (is_file($imagePath)) {
unlink($imagePath);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question