Nalaka526 Nalaka526 - 5 months ago 21
PHP Question

Get Root Directory Path of a PHP project

I have this folder structure in my PHP project.
(this is as shown in eclips)

-MySystem
+Code
+Data_Access
-Public_HTML
+css
+js
+Templates
-resources


When I try this code

echo $_SERVER['DOCUMENT_ROOT']


output is


D:/workspace


How can I get the path to RootDirectory of the system (
MySystem
), without hardcoding the Folder Name?

Answer

For PHP >= 5.3.0 try

PHP magic constants.

__DIR__

And make your path relatively.

For PHP < 5.3.0 try

dirname(__FILE__)