Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format a PHP include() absolute (rather than relative) path?

Tags:

On various pages throughout my PHP web site and in various nested directories I want to include a specific file at a path relative to the root.

What single command can I put on both of these pages...

http://www.example.com/pageone.php
http://www.example.com/somedirectory/pagetwo.php

...to include this page:

http://www.example.com/includes/analytics.php

This does not work:

<?php include('/includes/analytics.php'); ?> 

Does it matter that this is hosted in IIS on Windows?

like image 713
Zack Peterson Avatar asked Dec 05 '08 19:12

Zack Peterson


People also ask

How do you convert relative path to absolute path?

The absolutePath function works by beginning at the starting folder and moving up one level for each "../" in the relative path. Then it concatenates the changed starting folder with the relative path to produce the equivalent absolute path.

Should I use absolute or relative path?

Relative links show the path to the file or refer to the file itself. A relative URL is useful within a site to transfer a user from point to point within the same domain. Absolute links are good when you want to send the user to a page that is outside of your server.

What is absolute and relative path in php?

An absolute path refers to a file on the Internet using its full URL, e.g. "http://www.uvsc.edu/disted/php/webct/itr/index.php" A relative path assumes that the file is on the current server, e.g. "php/webct/itr/index. php".


1 Answers

You can just use include $_SERVER['DOCUMENT_ROOT'] . "/includes/analytics.php";

like image 101
J Cooper Avatar answered Oct 05 '22 15:10

J Cooper