How to get working site path and directory name in php?
Let the page path be: http://localhost/test/admin/index.php
Get the server name/site name of your website:
echo $_SERVER['SERVER_NAME'];
or,
echo $_SERVER['HTTP_HOST'];
This will print: localhost
Get the path of the page you are working on:
echo $_SERVER['PHP_SELF'];
This will print: /test/admin/index.php
Get only the directory name of the page you are working on, i.e. if you don’t want the file name to be displayed:
echo dirname($_SERVER['PHP_SELF']);
This will print: /test/admin
If you want to omit the ‘admin’ directory as well then you can use dirname() function two times:
echo dirname(dirname($_SERVER['PHP_SELF']));
This will print: /test
Finally:
echo “http://”.dirname($_SERVER['SERVER_NAME'].”".$_SERVER['PHP_SELF']);
This will print: http://localhost/test/admin
Enjoy PHPing :)