PHP: __DIR__ and __FILE__ and including 'config.php' files

 

I suggest defining it as a constant so that it can't be altered and is available no matter the scope.

define('PROJECTFILES',__DIR__.'/projectfiles/');

Assuming:
/config.php
/projectfiles/include_me.php

include(PROJECTFILES.'include_me.php

Keep in mind that __DIR__ will reference the directory that config.php is in. If config.php is the parent folder of projectfiles then the above will work.

 

__DIR__ is certainly what you are looking for. Use it to provide relative paths for required files. I would highly suggest using require_once or include_once for all library files.

if( !defined( __DIR__ ) ) define( __DIR__, dirname(__FILE__) );
require_once( __DIR__ . '/../../../config.php');
.
.
.
// you code here

Using the first line ensures you wont run into trouble with PHP 5.2 and back. This will allow you to include files based on directory structure instead of relative to the script called. Many frameworks use dirname(__FILE__) instead, but it is rather wasteful to run more than once. It is better to just add it somewhere with a check.

Tags