Include without full hard coded path

I have the following include

include "/home/voteforp/public_html/core/conf.php";

I have to hard code the path, because the file that uses this include, is called from all sorts of folders. I thought I could just do the following:

include $_SERVER["DOCUMENT_ROOT"]."code/conf.php";

which worked when calling the php files over the web in the normal way. But i have to call them from the command line as well via an exec call. When i try to run the page from a command line call (eg: "php myscript.php") the include line doesn't work unless i hard code the full path.

The reason why i can't hard code it, is that this entire application will be moved to various other home folders. I'd have to change the hard coded path each time I installed it in a new directory. I don't want to have to do that.

Anyone have any ideas of how I can do this?
Who is Participating?
incrediblejohnConnect With a Mentor Commented:
I know this sounds too simple but put the include file in the same directory as the page that includes it. Wa la!

include "conf.php";

What about:
chdir ("/myprogrampath/");
shell_exec("php -q ". getcwd(). 'myprogram.php');

micamediaAuthor Commented:
what is that going to do for me? I still have to know the full program path.. that point is that i'm trying to make it working WITHOUT having to have code in the full program path

Actually i the file "core/conf.php" has the full program path as a configuration variable. I don't mind having to put it there, but I don't want to have to put it anywhere else. The problem I am having is how do I get all the scripts to be able to find the configuration file without having to put the full path in the include..

Like i said  $_SERVER["DOCUMENT_ROOT"]."core/confi.php"   finds the path just fine unless i run the file via the command line..

is $_SERVER["DOCUMENT_ROOT"] different when running from the command line?

No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: incrediblejohn {http:#13869661}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

EE Cleanup Volunteer
No objection here! Thank you!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.