PHP: Get current directory name

You can easily get the current directory name at where the script is currently running. The below code will print the current directory name into screen.


$directory = dirname(__FILE__);
print $directory;


How the script works? Here __FILE__ holds the name of the currently executing file. And using finction dirname() its extracting the directory name component from it.

That means if your script name is "test.php", then __FILE__ has something like "c:\work\test.php". The function dirname() just removing that test.php from it, and returning "C:\work\"

Comments