1

I am creating a project wherein the user can upload his photo. This photo is stored in the folder "images/uploads/filename". When his profile is created and the photo is to be shown, I use the <img> tag with src as "images/uploads/filename", but the photo does not show up. If I manually copy the photo to an adjacent folder "images/abc/filename" and then use it as the source then it works.

How is this caused and how can I solve it? I need to use the same folder to upload and download photos.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
rakmo
  • 13
  • 4

2 Answers2

2

That can happen if you're running the webapp as an IDE project and are storing the uploaded images in the IDE's project space. Changes in the IDE's project folder which are performed externally (as by your servlet code) does not immediately get reflected in the deployed server's work folder. Only when you touch it (by refreshing the project) or by copying it (as you happen to have found out), then it will get reflected.

After all, storing uploaded files in the webapp's deploy folder is a bad idea. Those files will get all lost whenever you redeploy the webapp, simply because those files are not contained in the original WAR file.

You need to store them somewhere outside the webapp's deploy folder on a different fixed path like /var/webapp/uploads. You should not use relative paths or getRealPath() to create the File object around it. Just use a fixed path. You can always make the fixed path configureable as a context param setting, a VM argument, a properties file setting or even a JNDI entry.

Then, to serve it to the world wide web, just add exactly that path as another docroot to the server config. It's unclear what server you're using, but in Tomcat it's a matter of adding another <Context> to the server.xml.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

Make sure your have the correct path and include the image extension

just for testing, put your img tag inside the index.php

<img src="images/abc/filename.jpg">

/root/index.php

now put you image in to the your abc/ folder

/root/images/abc/filename.jpg

This should display the image.


if you have your img tag inside another folder in the root like below

/root/user/index.php

now when you use you img tag use this path (relative link):

<img src="../images/abc/filename.jpg">

note the beginning of the image path "../" this is used to go back to the root.


if the php or html file that is holding the img tag is even deeper, just remember to add ../ for every level, 2 folders deep whould be:

<img src="../../images/abc/filename.jpg">


Question was quite vague so hope this is what you are looking for.

let me know and if this is wrong, explain a little more and i will try to help :)

Wesley
  • 285
  • 1
  • 9
  • i tried testing as u mentioned [code] but the result is same. i am using jsp and i take the src of the image from the database in a string variable. string src = "image/abc/filename.jpg" ['code'] this works, but string src = "image/uploads/filename.jpg" ['code'] does not work the problem seems to be with the uploads folder. is it because i use the same folder to store the pic that the users upload?? – rakmo May 03 '12 at 19:30
  • have you made your uploads folder writable 0777 ? also in your upload script, how have you dealt with folder creation and uploading images to particluar folders...can you show your code block – Wesley May 03 '12 at 21:11