Usually I use this to define the path to directory which contains the scripts:
realpath(dirname(__FILE__))
so at the very beginning of the index.php I can put something like this:
define('SITE_PATH', realpath(dirname(__FILE__)));
Then I can use SITE_PATH inside any class anytime I need to get value of root dir, so If I need to get absolute path to 'images' directory, I would use
SITE_PATH_PATH . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR;
This works, but I saw another post where someone asked about the use of
$_SERVER['DOCUMENT_ROOT']
the $_SERVER['DOCUMENT_ROOT'] is doing pretty much the same thing as realpath(dirname(__FILE__)) right?
Now I am wondering, when should I use realpath(dirname(__FILE__)) and when to use $_SERVER['DOCUMENT_ROOT']?
Which one is the preferred way to get the path of my scripts root directory?








Bookmarks