Search code examples
phpinclude-path

How to include a file with the absolute path always using PHP


have a file that is included in a required file. the problem that I am having is the included file work only if the user on pages that are located on the root directory of the site. but if te user branch out to a sub directory they I get the error

failed to open stream: No such file or directory

this is how I currently include the file with in the required file.

include('classes/browser_detection.php')

so I tried couple of thing which did not work

First:

include('/cms/classes/browser_detection.php')

second:

include( dirname(__DIR__) . '/classes/browser_detection.php')

both of those options did not work. How can I get it to work without having to include the class in every page individually where the main file is required?


Solution

  • see answer: PHP Directories - Best Practice

    I like to use:

    config.php (sits in the root)

    define('ROOT_SYS',dirname(__FILE__).'/'); //$_SERVER["DOCUMENT_ROOT"] for root access
    

    this allows me to work on the site locally and on the hosting with no issues. In your case it would look something like:

    include(ROOT_SYS . 'classes/browser_detection.php')