Page 1 of 1
Relative paths + includes = nightmares
Posted: Tue May 20, 2008 8:55 pm
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?
Re: Relative paths + includes = nightmares
Posted: Wed May 21, 2008 6:08 am
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")
Re: Relative paths + includes = nightmares
Posted: Wed May 21, 2008 11:34 am
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.
Re: Relative paths + includes = nightmares
Posted: Wed May 21, 2008 12:17 pm
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
Re: Relative paths + includes = nightmares
Posted: Wed May 21, 2008 12:24 pm
by face1
Where would I do that define(), the top of every file?
Re: Relative paths + includes = nightmares
Posted: Fri May 23, 2008 10:57 am
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
Re: Relative paths + includes = nightmares
Posted: Fri May 23, 2008 12:28 pm
by RobertGonzalez
Is there a single common include file that is shared by all pages?