Root-relative paths

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Root-relative paths

Post by uberpolak »

Any ideas why include('pages/' . $sec . '.php'); works fine, but include('/pages/' . $sec . '.php'); gives an error? The pages folder is in the document root, and if possible, it's going to be much easier for me to use root-relatives than try to figure out what folder I'm in all the time (user-friendly URLs).
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

from what I know, php doesn't really use the document root when using the slash, instead it uses the root of the system.

if you want to be able to include something no matter which document you are on, you should add it to include_path on your php.in file

i would recommend creating a directory with all files you need to include called globals or something similar on the root document of every website and adding ./globals to the include_path on php.ini
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

That must be an install folder under root - it can't be the doc root if "/path/to/file.php" doesn't work.

Check DocumentRoot in httpd.conf.
Last edited by McGruff on Thu Mar 04, 2004 10:04 pm, edited 1 time in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

you could do this for your includes, its what i do

Code: Select all

$BasePath = str_repeat("../", substr_count(dirname($_SERVER["SCRIPT_NAME"]), "/"));

require ($BasePath."ocdaintranet/includes/db_connection.php");
Mark
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

or this:

Code: Select all

include $_SERVER['DOCUMENT_ROOT'].'/pages/'. $sec. '.php';
Post Reply