By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,867 Members | 1,981 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,867 IT Pros & Developers. It's quick & easy.

Problem in including any file in html while hosting

P: 52
Hi all,

First of all, I use linux (fedora 6) and apache to host
I have a problem of including any file in html while hosting only
Even a simple code like

[HTML]<img src="/home/user/abc.jpg" />[/HTML]
won't show the included image.

To make it more clear,
If I view same page just opening it (not hosting) like
file:///home/user/abc.html
It shows the image included.

But if I view the webpage using
http://localhost/abc.html
Then it won't show any images

I need to host the page, show how shall I include the image.
Actually, its not just problem with images only, but every type of file.

Thanks
Jan 5 '08 #1
Share this Question
Share on Google+
9 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Hi all,

First of all, I use linux (fedora 6) and apache to host
I have a problem of including any file in html while hosting only
Even a simple code like

[HTML]<img src="/home/user/abc.jpg" />[/HTML]
won't show the included image.

To make it more clear,
If I view same page just opening it (not hosting) like
file:///home/user/abc.html
It shows the image included.

But if I view the webpage using
http://localhost/abc.html
Then it won't show any images

I need to host the page, show how shall I include the image.
Actually, its not just problem with images only, but every type of file.

Thanks
Ok, so it works on your local machine but not the hosting machine. Stupid question, but when you uploaded the html page, did you also happen to upload the image(s) as well? If you don't, how will the html page know where to find them?

You would also have to change the path to wherever you put the images on the hosting server.

Also, no need to put your localhost URL in your threads. We can't view something that is local to your machine.

Regards,

Jeff
Jan 5 '08 #2

P: 52
Ok, so it works on your local machine but not the hosting machine. Stupid question, but when you uploaded the html page, did you also happen to upload the image(s) as well? If you don't, how will the html page know where to find them?

Thanks for replying. But, yeah, your question was somewhat stupid.
Let me explain my problem.

I'm using apache to host (locally) in my own PC (using fedora 6).

I have a html file which includes an image. (plz remember I'm fully aware that I should give complete path of the image and I have done that)

Now I can open this file in 2 ways.
1. Just double click this file to view it in some browser (firefox in my case).
2. OR put this file into the /var/www/html folder (which is default directory to host a website, as it is htdocs in windows) and view this file as htttp://localhost/filename.html in a browser.

Case 1 works very fine.
Case 2 is giving problem. Even I give full path to the image, it isn't showing the image. Only those images which are in same directory or sub-directories are being shown. I can't include anything outside this /var/www/html directory


I have only one PC and I'm testing in it and not hosting from any other server.
Help me solve this problem.
Thanks
Jan 6 '08 #3

P: 32
Your absolute URLS need to reference the web visible path, rather than the filesystem path

In your example you pointed images to
Expand|Select|Wrap|Line Numbers
  1. /home/user/abc.jpg
On your filesystem these images exist, but the web brower tries to access that location it is looking for
Expand|Select|Wrap|Line Numbers
  1. localhost/home/user/abc.jpg
which the server translates to
Expand|Select|Wrap|Line Numbers
  1. /var/www/html/home/user/abc.jpg
Which doesn't exist.

So while absolute paths are correct, they must be absolute to the server, not the filesystem.

I hope this helps.

You hit the nail on the head with this comment
I can't include anything outside this /var/www/html directory
Thats right you cant.
To do so you would need to make that directory readable by the webserver, and either direct it as a subdir of localhost or create a virtualhost for just that directory.

The only other option you have is to use a server side language such as PHP and fopen() the file on the server and parse it out.
The PHP file would still need to be somewhere under the webroot location though.
Jan 7 '08 #4

P: 32
also you dont NEED to use absolute URLs, relative ones are fine.
Jan 7 '08 #5

P: 52
Hey,
Thanks for clearing most of my doubts.
But stll I have a doubt.


You hit the nail on the head with this comment
I can't include anything outside this /var/www/html directory
Thats right you cant.
To do so you would need to make that directory readable by the webserver, and either direct it as a subdir of localhost or create a virtualhost for just that directory.
I have created a directory /usr/tmp/uploads
/usr has read and execute permissions for all
tmp has read, write and execute permissions for owner, group and others all.
and I have given read, write, and execute permission to the uploads directory as well.

Even then it's not showing the image.

Thanks
Jan 10 '08 #6

P: 32
for security, most webservers are configured not to access files that are outside of the documentroot.
therefore a web served file CANT access resources that are outside of the document root.

eg
root
/
documentroot
/var/www/html/

so if a file tries to access
[HTML]href="/"[/HTML]
it gets
/var/www/html/
not
/

this means there is no way to access
/var/
or /usr/
unless you configure the webserver documentroot to be something else. ( not recommended.)

In apache you can create multiple vhosts that have different documentroots, but then they will be different hosts eg
site1.local and site2.local and would be referenced like
site1.local/index.html
etc

On second thought, the best alternative is to create symlinks to files
for example

/var/www/html/lib/
could be a symlink to
/usr/lib/myframework/lib/

This way you could install one copy of your chosen resource, and have it available at both locations.
also means you could have

/var/www/html/application1/lib/
/var/www/html/application2/lib/
/var/www/html/application3/lib/

all being symlinks to
/usr/lib/myframework/lib/

this way the files are stored outside you webroot, but are available from relative paths WITHIN the webroot.

Hope that clears that up a bit.
Jan 12 '08 #7

P: 32
In your
/usr/tmp/uploads
example.

2 options.
You could move that folder into
/var/www/html/uploads/

Or
create a symlink from
/var/www/html/uploads/
to
/usr/tmp/uploads

That should work.

I dont know where my brain was last week. :D
Jan 12 '08 #8

P: 52
Thanks Garrow for the solution.
I am now convinced that I can't get outside of /var/www/html (except using symlinks).
Jan 13 '08 #9

P: 32
wait let me just reiterate that
you cant access files outside the webroot :D
Jan 14 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.