Answer the question
In order to leave comments, you need to log in
How to display an image in Yii2 and specify a list of allowed extensions?
There is a form several several text fields.
The last field is for files.
In the controller, this code => the picture is saved in the desired directory. and enters the img table field with the string type = zalman-z7-neo.200x200n.jpg
'img')->fileInput()
= $form->field($model,
if (Yii::$app->request->isPost) {
$model->load(Yii::$app->request->post());
$model->img = UploadedFile::getInstance($model, 'img');
$model->img->saveAs("img/category/computers/{$model->img->baseName}.{$model->img->extension}") ;
<?php if($computer->img): ?>
<p>test</p>
<img src="/web/img/category/computers/<?= $computer->img?>" alt="" width="200" height="200">
<?php endif; ?>
<img src="/web/img/category/computers/zalman-z7-neo.200x200n.webp" alt="" width="200" height="200">
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