Yii2 how to display images stored in a web folder

I have images saved in / web / uploads / PROJECT in Yii2. How to display them on the page? below are my codes, but the image is broken.

Controller:

$img = Yii::$app->request->baseUrl.'/web/uploads/PROJECT/'; $image = Html::img($img.$img_obj['AVATAR'],["width"=>"600px"]); 

View:

 echo $data; 

Do I have to set any rights to display it? Right-clicking and opening the image in a new tab shows the URL "/web/uploads/PROJECT/3fioobapJ5vRk_wdCEzDJbQWyO66inWO.jpg", which seems to be correct, but the page displays "Not Found (# 404)"

Can anyone advise? Thanks!

+5
source share
3 answers

Strange, using the codes below, it displays images correctly.

 $img = Url::to('@web/uploads/PROJECT/').$img_obj['AVATAR']; $image = '<img src="'.$img.'" width="600" />'; <img src="<?= Yii::$app->request->baseUrl . '/backend/web/uploads/' . $model->profile_photo ?>" class=" img-responsive" > <?php echo Html::img('@web/img/icon.png', ['class' => 'pull-left img-responsive']); ?> 
+9
source

Static images should be placed inside web / any_folder_name

to access YII 2:

 <?= Html::img('@web/any_folder_name/bg1.jpg', ['alt'=>'some', 'class'=>'thing']);?> 
+4
source

Add this code to your grid view:

  [ 'attribute'=>'photo', 'value' => Html::a(Html::img(Yii::getAlias('@web').'/image/'.$st_data->photo, ['alt'=>'some', 'class'=>'thing', 'height'=>'100px', 'width'=>'100px']), ['site/zoom']), 'format' => ['raw'], ], 
+2
source

Source: https://habr.com/ru/post/1212595/


All Articles