Universal path to a specific file or folder in php

Posted on 2009-04-27
Last Modified: 2013-12-12
I need to build a site on my server, and then changing to my client server with no need to change the paths, just to move the files.
Ex.: I have the config file in one folder, and the path for this template should work no matter the server is. Or I have the images folder for the galery on the root, and I neeed acess this folder from several pages, no matter where the site it is located.
Question by:WiseGoose
    LVL 6

    Expert Comment

    For this you want to set up some variables in your script. By using the values of __FILE__ and      DIRECTORY_SEPARATOR constants, you should be able to include or require any files your applicaiton needs without knowing an absolute path.
    LVL 6

    Expert Comment

    To clarify, as an example you could do something like this in your script:

    $application_root_directory = __FILE__;
    $includes_directory = $application_root_directory .  DIRECTORY_SEPARATOR . 'includes' .  DIRECTORY_SEPARATOR;
    require($includes_directory . 'include_me.php');

    Open in new window


    Author Comment

    Hi basic612,

    Thanks. Suppose that I have a file called configure.php and I need to load it for all my scripts. This file is at the "includes" folder, wich is at a root level. I need to put the file at the top of all my scripts, and then change the server, without modify the script.
    How do you adapt your script?

    LVL 3

    Expert Comment

    You could also use the super global $_SERVER['DOCUMENT_ROOT']
    LVL 6

    Expert Comment

    If all of your scripts are at the root level of your application apart from the configuration file which is in includes, you can just use relative paths:

    include ('includes/configuration.php');

    or for better portability:

    include ('includes' . DIRECTORY_SEPARATOR . 'configuration.php');

    I just find it simpler typing


    instead of

    'includes' . DIRECTORY_SEPARATOR

    each time I want to call an include file.

    I personally would avoid using the $_SERVER['DOCUMENT_ROOT'] global in case your application gets installed in a subfolder of the document root. You can;t control where the folder is placed and the scripts called from, however you can expect the relative structure of your directories and files to remain constant.

    Author Comment

    Works fine your idea.
    One last thing: there is a special reason for use a constant to the DIRECTORY_SEPARATOR?
    LVL 6

    Accepted Solution

    That is to ensure compatibility between Windows and Unix which use different separators:

    // Linux:

    // Windows:

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    732 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

    25 Experts available now in Live!

    Get 1:1 Help Now