Include without full hard coded path

Posted on 2005-04-23
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
    LVL 7

    Expert Comment


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

    LVL 1

    Author Comment

    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?

    LVL 3

    Accepted Solution

    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

    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
    LVL 3

    Expert Comment

    No objection here! Thank you!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Preface In the first article: A Better Website Login System ( I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
    Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
    The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    758 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now