Saturday, November 20, 2010

PHP: Get current directory name

Share at Facebook

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\"

No comments: