V
V
Vladimir2022-04-19 21:42:29
Yii
Vladimir, 2022-04-19 21:42:29

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
<?= $form->field($model, 'img')->fileInput()?>

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}") ;


I'm trying to get her out.
<?php if($computer->img): ?>
                            <p>test</p>
                                <img src="/web/img/category/computers/<?= $computer->img?>" alt="" width="200" height="200">
                            <?php endif; ?>

if works for me, test is displayed, but the picture does not show.
625f012f0026a797679186.png

in debug it shows like this. i.e. the path is set.
<img src="/web/img/category/computers/zalman-z7-neo.200x200n.webp" alt="" width="200" height="200">


and how to specify extensions, not rules (it didn’t work out in them), but in some other way, for example, pass it to the fileInput value (HERE for example) if this is possible

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2022-04-20
@faradeifrontend

Decided to change the path.

<img src="/img/category/computers/zalman-z7-neo.200x200n.webp" alt="" width="200" height="200">

and specifying $model->img->saveAs after the validation check.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question