Get 2 levels up from dirname( __FILE__)

levi picture levi · Jun 28, 2011 · Viewed 41.3k times · Source

How can I return the pathname from the current file, only 2 directories up?

So if I my current file URL is returning theme/includes/functions.php

How can I return "theme/"

Currently I am using

return dirname(__FILE__)

Answer

Charles Sprayberry picture Charles Sprayberry · Jun 28, 2011

PHP 7

return dirname(__FILE__, 2);

PHP 4 and higher

return dirname(dirname(__FILE__));

With PHP7 you can get further up the directory tree by specifying the levels parameter, while pre-7 PHP will require further nesting of the dirname function.

http://php.net/manual/en/function.dirname.php