0

I'm having issues with a small and trivial application that I created to help me learn Symfony 3. The Application simple allows me to upload an image when I create a new blog post.

I'm using the Symfony 3 documentation to learn about uploading file, and in it, it suggests that I rename the file to a unique name using the md5() hash method to request a strange looking name for the file.

Everything works find until I want to display the image in the index.html.twig file. I was under the impression that I could simply use the asset() method that I was using to import bootstrap and other publicly viewable files, and then simply append the file name to the end of that. But nothing seems to work.

Here are some things that I have tried.

<img src="{{ asset('blog/images/') }}{{ blog.imageUrl }}">
<img src="{{ asset('blog/images/', { 'imageUrl': blog.imageUrl }) }}">
<img src="{{ asset('blog/images/' . blog.imageUrl) }}  ">

And there is probably a few more different combinations that I have tried but can remember. Now I have since found the documentation that talks a little about the asset() method, but there is nothing there that seems to state that you can use it and then just append the file name to the end of it, And I haven't found any information that allows me to point directly to the web directories, where the blog/images folder now lives. So any help with this would be awesome. Thank you so much.

Kaley36
  • 233
  • 9
  • 19
  • What is `blog.imageUrl` value? Have you checked what's the result HTML of your code? Have you tried to access an example image file directly through url in browser? – Jakub Matczak May 09 '17 at 06:02

2 Answers2

0

Not knowing the content of blog.imageUrl its hard to know what is expected. My guess for the 'correct' answer to your question is that you were very close with option #3, however in twig string concatenation is done using the ~ symbol and not .

So in your case you would use <img src="{{ asset('blog/images/' ~ blog.imageUrl) }}" />

Regards

Jenne
  • 864
  • 5
  • 11
0

Make sure that the new name of the file matches the name stored in the database. That and make sure the path you're using is correct.