Posted on 2011-10-03
Last Modified: 2012-05-12
I have the below code which basically goes to a file which connects to my mysql database.


How this code is within a template and I want folder destination to work regardless of the folder level of the connecting file.

So I though the below would work so starts at the root.


but it failed and so did the below:


Why is this? I would have thought an absolute path would be fine?
Question by:petewinter
    LVL 11

    Expert Comment

    Is that an absolute path on the server or for the url? I'm guessing there is more to the path before /Connections/ on the server. This needs to be included for it to be an absolute path on the server.

    Author Comment

    That is the absolute path for the url

    Does the require_once() command work differently to the include() command.

    What's the difference?
    LVL 11

    Accepted Solution

    The don't work differently with regard to the path. If you don't prepend it with some sort of protocol designation (like http or ftp), they will both assume it's local path on the server.

    The difference between include and require is that include will continue with a warning if the file doesn't exist. Require will error out. require_once has the benefit that it will only attach the file to a running script once if for some reason it is included or required elsewhere in the same script. Including or requiring the same file in the same script will cause an error as well.
    LVL 11

    Expert Comment

    Prepending your path with $_SERVER['DOCUMENT_ROOT'] may fix your issue depending on your server configuration.

    LVL 13

    Expert Comment

    Using an absolute path works, but the directory you are using is not there.

    If you are on linux server, you will need to also include the home directory like:


    also, if you try to include via http like your 2nd example, this causes the php script to be executed before its included so you won't be able to use the classes this way, you must use relative path.

    To find relative path use realpath()
    LVL 14

    Expert Comment

    by:Scott Madeira
    If conn_xxx.php is always going to be the same but just called from different directories then why don't you put the file somewhere in the Include path and then all the directory problems go away.  Just do:


    For security purposes you could put it outside of the document root so it isn't accessible by a URL but is is accessible by the file path in the include statement.


    Author Closing Comment

    Many thanks for your help.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    The viewer will learn how to dynamically set the form action using jQuery.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    730 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

    16 Experts available now in Live!

    Get 1:1 Help Now