How to get pictures outside of folder web

Hi guys,

I try to get pictures,which are not in subfolder of web.If i code like this, image will be shown up




<?= Html::img('@web/pictures/tklustig.jpg', ['alt' => 'pic not found', 'style' => 'width:50px; height: 50px']); ?>



In this case, picture won’t be shown up. It will be shown alt-tag instead




 <?= Html::img('@uploading/tklustig.jpg', ['alt' => 'pic not found', 'style' => 'width:50px; height: 50px']); ?>



In both folders are several pictures, also tklustig.jpg!!


echo "<font color='red'>" . Yii::getAlias('@uploading');

will show this:


E:\xampp\htdocs\yii2_perswitch\frontend/uploadedfiles

Any ideas,how I can show up pictures,which are in alias folder using img()-method of html class or another method?

Best way is putting images to web directory.

Other way is to create symlink:




// windows

mklink /D /J C:\OSPanel\domains\site.dev\backend\web\images C:\OSPanel\domains\site.dev\frontend\web\images

// debian, ubuntu, centos

ln -sv /var/www/frontend/web/images/ /var/www/backend/web/



I’d recommend symlinks :)

That way you don’t clutter your web directory unnecessarily.

Hi,

I suggest you make your life easy and follow the framework!

  1. If you want to use @uploading or any other undefined Alias, be sure you have defined it in the appropriate config file depending on which application template you are using

  2. Make sure the image is actually in the path you have mapped to the Alias.

  3. Resolve the alias before passing it to any methods in this fashion

$path = Yii::getAlias(’@uploading/tklustig.jpg’);

  1. Then use the resolved path in your call.

<?= Html::img($path, [‘alt’ => ‘image’, ‘style’ => ‘width:50px; height: 50px’]); ?>

Hope I understood your question and I hope this helps.

If you are still having issues open your debugger and set breakpoints to match the steps above and take a look at the variables

Adam

That’s actually great advice!

To 1.:

I defined alias like this:


  return [

        'aliases' =>

        [

            '@uploading' => '@app/uploadedfiles'

        ],

.

.

.



To 2.:I’m sure I have tklustig.jpg in this folder

To3.:I defined $path like this:


$path = Yii::getAlias('@uploading/tklustig.jpg');

To4.:I try to get image like this,but i don’t succeed.alt tag will be shown,instead


<?= Html::img($path, ['alt' => 'pic not found', 'style' => 'width:50px; height: 50px']); ?>

I can’t use symlinks,'cause every programmer of our team should set them. That’s impossible!

I solved problem copying pictures to web folder like this





<?php

$folder_read= Yii::getAlias('@uploading/');

$folder_write = Yii::getAlias('@pictures/');

$upload_copy = opendir($folder_read);

while ($file = readdir($upload_copy)) {

    if ($file != "." && $file != ".." && $file!=".gitkeep")

        copy($folder_read . $file, $folder_write . $file);

}

closedir($upload_copy);

?>