Setting Your Include Path

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
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Setting Your Include Path

Post by omniuni »

As you include things, the relative include paths are relative to the current document. I have tried re-writing the script with absolute paths, but that did not work. Can I set a script to execute setting the current path relative to it?

In other words

I have index.php, a folder "includes" and in there, "db.inc" and "readdb.inc". From index.php, I want to include "includes/readdb.inc", and I want to be able to access "db.inc" from "readdb.inc" using the relative path simply "db.inc".

Thanks for any help...

-OmniUni
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Re: Setting Your Include Path

Post by Phoenixheart »

No, I don't think so. The path is relative to the executing script, in this case index.php. In other words, once you include db.inc into index.php, it becomes a part of index.php, so a reference to "readdb.inc" will look in the same folder of index.php... and fails :)
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Setting Your Include Path

Post by omniuni »

Thank you.

Apparently it is something about the way the script I am using is written; I went through and set relative paths as from the executing script by hand, and it worked.

I had tried creating a variable to use as the path, and that failed miserably, for some reason.

Thanks again,

OmniUni
andychamberlainuk
Forum Newbie
Posts: 6
Joined: Mon Jul 28, 2008 7:15 pm

Re: Setting Your Include Path

Post by andychamberlainuk »

if you want absolute links, you must include your entire server path (whether it be local or on a server)

Here's the code I use: -

Code: Select all

 
include_once ($_SERVER['DOCUMENT_ROOT'] . "/classes/mysql.php");
 
By using this, you can be sure that you are including the correct documents EVERY time!
Post Reply