0
votes

I am using Windows 10 and trying on my local. From backend, I am able to successfully upload image in backend/web/uploads folder and on frontend I tried to access via following code in gridview.

['attribute'=>'manufacturer_logo', 'label'=>'Logo', 'format' => 'html', 'value'=>function ($searchModel, $index, $widget) { return Html::img(Url::to('@web/uploads/' . $searchModel->manufacturer_logo),['width' => '50px']); }],

When I see image path from console then path looks like

/myprojectname/backend/web/uploads/home_page_default_image.png

I also tried path with http like

http://localhost/myprojectname/backend/web/uploads/home_page_default_image.png

but image is not showing, it is present in the folder, I am not able to find out what is the mistake?

2

2 Answers

0
votes

Your code looks good. So, a short checklist:

  1. Verify that the file exist
  2. Try the URL to the file directly on browser, the image have to appear. If not check permissions but as you are on windows, I don't think this to be the issue.
  3. You don't need to use Url::to inside Html::img. This one knows how to interpret @web alias

Remember, you save with path but to show you need to use url. So, you need to make sure you are pointing to the same location.

And this one here is the behavior we created to forget about upload issues. https://github.com/daxslab/yii2-uploader-behavior.

0
votes

When you call @web in backend it loads domain/backend/web, but when you call it in frontend - it's domain/web or domain/frontend/web (I don't know if you have some htaccess rules) so here is your problem. Image url is with backend.

You just set path like Url::to('/backend/web/uploads/' . $searchModel->manufacturer_logo)

or use another way to concatenate backend before all.

Also you can set new alias. In common/config/bootstrap.php add this Yii::setAlias('@backendweb', '/backend');, so your image url will be:

Url::to('@backendweb/uploads/' . $searchModel->manufacturer_logo)