3

Little bit confused here. I have looked at a few threads and they are not solving it.

1)  <img src="/web/uploads/ce438b2eb1c01d85f3d6d6c52efc1175.png"/>

2)   <img src="{{ asset('/uploads/ce438b2eb1c01d85f3d6d6c52efc1175.png')}}"/>

Edit:

2) Works
1) does not.

The crazy thing is, in PHPstorm, if I do /web in the src route it selects it for me, then it selects uploads for me and can even see the file. It just wont display!?

The below works for me, it displays the image. So there is no real rush on this now. However it is interesting to why 1) doesn't work?

  <img src="{{ asset('/uploads/' ~ media)}}"/>
  • What version of Symfony you use? Didn't use in some time, but don't you should have `/upload` folder as main folder, not inside bundle? [Cookbook](http://symfony.com/doc/current/controller/upload_file.html) on Symfony page. – timiTao May 16 '17 at 11:26
  • It is in my bundle as the docs suggested to do. –  May 16 '17 at 11:28
  • I believe I had a mistake. Let me edit. –  May 16 '17 at 11:29
  • I have updated the code. One of my methods was incorrect as I was looking i the bundle when the folder is not in the bundle. I still get a 404 error. –  May 16 '17 at 11:31
  • Ok, but what is you aim? To show image somehow? to show it by controller? What is content of `MyBundle:Database:mediaView.html.twig` – timiTao May 16 '17 at 11:38
  • Updated. That is the only content timi, image tags. –  May 16 '17 at 11:45

4 Answers4

0

You have in your code:

< img src="mySite/web/uploads/{{media}}"/>

But probably you domain is set on folder project mySite/web that shouldn't not correct from browser. Then you final link, should be

< img src="mySite/web/uploads/{{media}}"/>

or

< img src="{{ asset('/mySite/web/uploads/{{media}}')}}"/>

If this won't help, then maybe someone more experienced could help.

timiTao
  • 1,417
  • 3
  • 20
  • 34
  • 1
    In your controller, you assigned string, to `file`. Now you try to use this as `array`. Fix your code :) – timiTao May 16 '17 at 11:57
  • Yes you are right again. But that was a typo. I am not just interested to why 1) dosn't work. But thanks. –  May 16 '17 at 12:04
0

if 2. works it should be:

  1. <img src="/uploads/ce438b2eb1c01d85f3d6d6c52efc1175.png"/>

  2. <img src="{{ asset('/uploads/ce438b2eb1c01d85f3d6d6c52efc1175.png')}}"/>

But that means your vhost root is not set correctly is should point to /web but it seems it points to the parent of /web which is the root of your Symfony project.

COil
  • 7,201
  • 2
  • 50
  • 98
0

web/ is a basic directory web files, and here you start search you front-ent file: [img, js, css]

0

The answer should be easy to find by searching the docs.

<img src="/web/uploads/ce438b2eb1c01d85f3d6d6c52efc1175.png"/>

doesn't work, simply because, if this code you've wrote it inside, let's say, AppBundle/Resources/views/Default/index.html.twig file, then this means the web/uploads/ directory structure should be present inside this directory (AppBundle/Resources/views/Default/) - and having the image inside it.

Which is not your case. So for AppBundle/Resources/views/Default/index.html.twig, the src should be:

<img src="../../../../../uploads/ce438b2eb1c01d85f3d6d6c52efc1175.png

That's why the asset() function is useful. Because it maps the path to your public directory (declared in composer.json file, under the symfony-web-dir key); so, no matter from where you are calling this function, it will always return the path to the public directory, so you don't have to worry about all those ../../ inclusions.

Dan Costinel
  • 1,716
  • 1
  • 15
  • 23