0

Possible Duplicate:
Absolute path & Relative Path

This is a very basic question, but I have been scratching my head over this for a long time and I am hoping someone can help clear up my confusion.

I have a shared account on a linux server.
The path to public_html is: /home/myusername/public_html The code for my website lives here: /home/myusername/public_html/mysite.com Under mysite.com there are directories such as 'mycss', 'myjs', 'myphp', etc. When I create an absolute path (for example, within php code or an html file), sometimes the path needs to start at /home, eg /home/myusername/public_html/mysite.com/myphp/myfile.php And sometimes it needs to start inside mysite.com, eg /myphp/myfile.php

My confusion: When does an absolute path need to start at /home, and when does it need to start within mysite.com? Is there a rule of thumb, or some insight anyone can give?

Community
  • 1
  • 1
moondog
  • 1,537
  • 5
  • 22
  • 34
  • Usually you can always use absolute path but cannot always use relative path. So it's safe to allways start at /home/, even in cases when you can start at /myphp/ – Dmitri Snytkine Feb 14 '12 at 21:06
  • NOT A DUPLICATE. This was not a general question about absolute and relative paths. The question was about when to use the ABBREVIATED absolute path versus the FULL absolute path, which was answered by Jakub below, and is not addressed in http://stackoverflow.com/questions/181805/absolute-path-relative-path – moondog Feb 14 '12 at 22:01

3 Answers3

1

You are confusing two types of absolute / relateive.

IF you are speaking in website terms (images / js / html stuff), your absolute is the path from your root website folder (ie: /home/myusername/public_html/mysite.com)

So if you have a folder like:

/home/myusername/public_html/mysite.com/images/bob.jpg

In your HTML, the absolute path would be /images/bob.jpg, while a relative website path would be images/bob.jpg etc;

IF you are doing PHP / Server side includes, you need to use the FULL absolute path, so your whole /home/myusername/public_html/mysite.com stucture.

But also at the same time, if you want to use a relative method (preferred in most cases as it makes your PHP code easier to move) you would just do an include('../database/connect.php');

Jakub
  • 20,418
  • 8
  • 65
  • 92
  • Thank you, this is very helpful. If I understand correctly, you are saying that for client-side code (eg html, javascript), use the abbreviated absolute path. For server-side code (eg php, htaccess), use the FULL absolute path. Is that right? (Also, thanks for the tip on using relative paths instead.) – moondog Feb 14 '12 at 21:26
  • correct, as your 'website stuff' is reiterative to where the website files are. The confusion lies with your PHP scripts as they just live inside the folder structure, and their 'root' is the `/` or `c:\` etc; – Jakub Feb 14 '12 at 21:43
0

It depends on the website. Did you write the PHP code for the website? Usually the absolute path will point to the web root (eg. /home/myusername/public_html/mysite.com ), but if you have config or other included files that are outside of the web root for security purposes - that might require a different path (eg. /home/myusername/notwebroot )

user1161625
  • 642
  • 1
  • 8
  • 13
0

You're confusing what is actually a relative path with an absolute path.

/home/myusername/public_html

is an absolute path

/public_html/anything
/myphp/myfile.php
/mysite.com/anything

these are all relative paths.

If it isn't starting at the root then it isn't an absolute path. If it is starting at the root of your website then it is a relative path because if you move your website to a different folder then the paths to these directories and files don't need to change (i.e. they are relative to the web root).

Brian
  • 2,229
  • 17
  • 24
  • Funny that basically the same answer as the upvoted/accepted answer regarding relative and absolute paths and someone downvotes it. – Brian Feb 14 '12 at 22:16