re: php relative paths to files using includes

Posted on 2009-04-29
Last Modified: 2012-05-06
i have 2 files:




both are using the same include file with relative links to say images/image.gif

can i set up some sort of configuration file in my root directory that somehow defines the a path so i don't have to make "images/image.gif" absolute in (i.e. - ""??

if so please explain thoroughly - i've never done this be for (please be gentle LOL)
Question by:phillystyle123
    LVL 5

    Expert Comment

    Hi phillystyle123,

    that's not hard to do.

    Create a file config.php, and define the path to the image directory there, like:


    $image_dir = "[absolute_path]/images/";


    Put in the absolute path instead of [absolute_path].

    Then just include the config file in both index.php files:

    In /index.php:


    include 'config.php';


    In /subdirectory/index.php:


    include '../config.php';


    Best regards,


    Author Comment

    hmm - i have it set up but it doesn't make sense to me - am i supposed to add that variable to each file path:

    like $image_dir/bla.gif  ?

    i've got this in my config file:


    $image_dir = "usr/home/healthpresence/orthoneuro/images/";


    and then this at the top of both index.php files:

    <?php include('includes/config.php');?> and <?php include(../'includes/config.php');?> but it's not working yet

    LVL 5

    Accepted Solution


    You are using a linux/unix based OS, and I'm sure there is the trailing slash missing in the absolute path:

    $image_dir = "/usr/home/healthpresence/orthoneuro/images/";

    Maybe this was already the only problem.

    I assume that the config.php file is in the dir /usr/home/healthpresence/orthoneuro/includes/.

    > am i supposed to add that variable to each file path:
    > like $image_dir/bla.gif  ?

    Yes, PHP won't guess where the image resides. ;-)

    If you use the $image_dir variable within a function, don't forget to globalize it by putting
    "global  $image_dir;" at the top of the function body:

    function blabla (..) {

      global  $image_dir;




    Author Closing Comment

    that's funny - i'd actually already done this in a slightly different way - thanks tons - much apprecated

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    Don't send email 6 40
    Problem to echo 6 28
    php explode 3 28
    how can I referesh my page? 4 15
    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    The viewer will learn how to count occurrences of each item in an array.
    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 …

    779 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

    11 Experts available now in Live!

    Get 1:1 Help Now