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?
Relative paths + includes = nightmares
Moderator: General Moderators
- slightlymore
- Forum Newbie
- Posts: 9
- Joined: Sun May 18, 2008 5:24 pm
- Location: Oxford, England
Re: Relative paths + includes = nightmares
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")
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
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
i use something like this
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
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
Where would I do that define(), the top of every file?
Re: Relative paths + includes = nightmares
oh -- you are running spaghetti codes =X
unfortunately, yes, if the system is not centralized in some way you'll have to do that
unfortunately, yes, if the system is not centralized in some way you'll have to do that
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: Relative paths + includes = nightmares
Is there a single common include file that is shared by all pages?