Include without full hard coded path

Posted on 2005-04-23
Medium Priority
Last Modified: 2013-11-18
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?
Question by:micamedia

Expert Comment

ID: 13851388

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


Author Comment

ID: 13851754
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?


Accepted Solution

incrediblejohn earned 2000 total points
ID: 13869661
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";
LVL 14

Expert Comment

ID: 15990309
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

Expert Comment

ID: 16018395
No objection here! Thank you!

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question