Forum Moderators: coopster

Message Too Old, No Replies

Linking to root

         

rwilson

6:25 pm on Oct 3, 2011 (gmt 0)

10+ Year Member



How can I make a link that always refers to the site root. I have an image in an include, and the include appears in many different directories. What's the best way to refer to that image? Any help is greatly appreciated!

g1smd

6:28 pm on Oct 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use
a href="/the/path/to/the/file.ext"
with the image reference starting with a slash.

rwilson

7:25 pm on Oct 3, 2011 (gmt 0)

10+ Year Member



Thank you!

rocknbil

4:07 pm on Oct 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just to clarify - for PHP includes and file references, you want the full system path

/var/www/example.com/public_html/mydata/file.ext

Which can be found in $_SERVER['DOCUMENT_ROOT']

$path = $_SERVER['DOCUMENT_ROOT'] . '/mydata/file.ext';

(exactly the same as the hard coded example above)

But for HTTP requests, such as images, css, etc., you want the root of the domain. So if in the previous example the site is example.com, that would start at public_html (usually:)

/mydata/file.ext

rwilson

5:02 pm on Oct 4, 2011 (gmt 0)

10+ Year Member



very cool, that may come in handy soon!

Readie

7:37 pm on Oct 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a quick note:

$_SERVER['DOCUMENT_ROOT'], depending on server setup, may or may not have have a trailing slash, so make sure there isn't one by doing:

rtrim($_SERVER['DOCUMENT_ROOT'], '/')