Relative paths + includes = nightmares

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
face1
Forum Newbie
Posts: 3
Joined: Tue May 20, 2008 8:46 pm

Relative paths + includes = nightmares

Post by face1 »

I'm presently updating a (poorly made, by someone else) cms, so that it is more portable. There is a folder called PHP_COMMON, and within it are a whole bunch of scripts that are not only included by outside scripts but also by each other (and no, it is not a flat directory). Every script that includes something from the PHP_COMMON directory uses the full, explicit path (/home/username/directory/PHP_COMMON/whatever.php), but I want to have no explicit paths in any of the scripts, because I need to be able to move the entire collection of scripts to different folders/servers. My first thought (well, actually I did this, only to realize that it obviously wouldn't work) was to go through and replace each require_once(/explicit/path/PHP_COMMON/stuff) with require_once(../../PHP_COMMON/stuff) (of course using the correct amount of ../'s). Unfortunately, there are files included by other files (in different directories!) that make these calls, so the amount of ../'s is off.

What is the best way for me to easily get the correct path to a directory, without having explicit paths?
User avatar
slightlymore
Forum Newbie
Posts: 9
Joined: Sun May 18, 2008 5:24 pm
Location: Oxford, England

Re: Relative paths + includes = nightmares

Post by slightlymore »

take a look at the chdir function http://www.php.net/function.chdir

you can change paths relatively (i.e. chdir (../../PHP_INCLUDES) then just include files from that directory by saying inlcude("./file.php")
face1
Forum Newbie
Posts: 3
Joined: Tue May 20, 2008 8:46 pm

Re: Relative paths + includes = nightmares

Post by face1 »

No...that doesn't help, because it is essentially the same thing that I had. The whole point is that I do not necessarily know the relative path to PHP_INCLUDES, asnecessarily scripts include each other, and then include something in PHP_INCLUDES, and the current working directory is the directory of the "outermost" script running.
User avatar
puke7
Forum Newbie
Posts: 12
Joined: Sat May 10, 2008 11:49 am

Re: Relative paths + includes = nightmares

Post by puke7 »

i use something like this

Code: Select all

define('LOCAL_DIR', $_SERVER['DOCUMENT_ROOT'].'/');

then you can truncate your file addresses from '/home/username/directory/PHP_COMMON/whatever.php' to LOCAL_DIR.'PHP_COMMON/whatever.php'
i use this method both locally and far, far away in server land while working on a system
face1
Forum Newbie
Posts: 3
Joined: Tue May 20, 2008 8:46 pm

Re: Relative paths + includes = nightmares

Post by face1 »

Where would I do that define(), the top of every file?
User avatar
puke7
Forum Newbie
Posts: 12
Joined: Sat May 10, 2008 11:49 am

Re: Relative paths + includes = nightmares

Post by puke7 »

oh -- you are running spaghetti codes =X

unfortunately, yes, if the system is not centralized in some way you'll have to do that
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Relative paths + includes = nightmares

Post by RobertGonzalez »

Is there a single common include file that is shared by all pages?
Post Reply