Get Current Directory Function in PHP

Posted on

Something that many people ask for is an extremely simple piece of code and that is “How do I get the current directory in PHP?” Well, we use a function to do this and the code is as follows:

function getDirectory() {
 $f = getcwd();
 return substr($f, strrpos($f, "/") + 1);
}

The PHP function getcwd() returns the current working directory and path. The following line simply strips the path and returns the name of the current directory. Instead of using getcwd() you can also use dirname(__FILE__) so the code could be rewritten as:

function getDirectory() {
 $f = dirname(__FILE__);
 return substr($f, strrpos($f, "/") + 1);
}
This entry was posted in Web Development. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *