PHP include paths

Posted on 2004-12-01
Last Modified: 2013-11-18
More of a general one this. I've got a bunch of classes that I use day to day. I've been playing with one of them, and I have tried to include another file at the top of the class containing some configuration options.

My path is like this:



So, the index includes the class, and the class includes the

However, am I right in thinking, that for the class to include the, it will start the path from where *it* was originally called? i.e if my class file simply has include(''); it will in fact try to call it in from the root directory, not where the class itself is stored?

What's the most flexible way around this?
Question by:andycal
    LVL 9

    Expert Comment

    you can include a file that is not in a root dir where index.php is like this


    LVL 48

    Expert Comment

    if you want to include a file once use
    require_once or include_once, so such a php-file is only included once, even if its called with require_once multiple times.

    I typical have a fill which includes all nexessary thing e.g


    // make settings

    //include classes and function

    require_once 'includes/';
    require_once 'includes/';

    on the pages i use the only do a
    require_once 'config.php';

    Author Comment

    That's kinda relying on knowing always the path, I guess what I'm after is a way of ensuring that the config file is called into the class file, always using the class file path.
    LVL 15

    Expert Comment

    you can also set your include_path directive in the php ini file such so that PHP searches for included files in the way that you want. You can do this in your script at runtime using ini_set():

    For information on include_path:

    From the PHP manual:
    Files for including are first looked in include_path relative to the current working directory and then in include_path relative to the directory of current script. E.g. if your include_path is ., current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/ and then in /www/include/.

    LVL 9

    Expert Comment

    To this you just have to set up include path, where your includes reside like this

    then you include those files just like if they were in root
    That way your include will be the same no matter in what direcory file is.

    LVL 49

    Accepted Solution

    You can use the statement below in your to always include the config correctly, even when you are not sure about the original basepath:


    __FILE__ contains the actual absolute path to /root/includes/, therefor dirname(__FILE__) returns /root/includes.

    Even when you move the files, but they both stay in the same directory, this method still works.



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    Shoutout to Emily Plummer ( for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    761 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

    8 Experts available now in Live!

    Get 1:1 Help Now