1

I want to make a path to be used with links and src attributes in my html - how do i achieve this?

my attempts:

<a href="' . $_SERVER['DOCUMENT_ROOT'] . '">Home</a>

or

<img src="' . $_SERVER['DOCUMENT_ROOT'] . '.images/whatever.jpg">

Can anyone expound upon this?

Draaken
  • 63
  • 7
  • 3
    it needs to be echo'd, if you're not already doing so. – Funk Forty Niner Nov 18 '15 at 16:31
  • What exactly do you want to know? – Manuel Fuß Nov 18 '15 at 16:33
  • Possible duplicate of [Difference between $\_SERVER\['DOCUMENT\_ROOT'\] and $\_SERVER\['HTTP\_HOST'\]](http://stackoverflow.com/questions/7229783/difference-between-serverdocument-root-and-serverhttp-host) – developerwjk Nov 18 '15 at 16:35
  • no its not a duplicate - i simply want to know how to make a dynamic link that always goes to my root folder of my website, that is all - i know im not good at asking these questions - but that is what i want – Draaken Nov 18 '15 at 16:56

1 Answers1

1

$_SERVER['DOCUMENT_ROOT'] returns that path to the document root on the server, for instance: /var/www/my.website.org/src or C:/wamp/www/my.website.org/src.

You shouldn't use it to make URLs directly, you can use it to make relative paths, for instance:

$root = $_SERVER['DOCUMENT_ROOT'];
$file_path = __FILE__;
$relative_file_path = str_replace($root, "", $file_path); // needs better logic but works in some cases

$base_url = "http://my.website.org/";
$url = $base_url . $relative_file_path; // http://www.mywebsite.org/path/to/file.php
Halcyon
  • 57,230
  • 10
  • 89
  • 128