Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get 2 levels up from dirname( __FILE__)

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__) 
like image 926
levi Avatar asked Jun 28 '11 18:06

levi


People also ask

What is dirname (__ file __)?

dirname(__FILE__) allows you to get an absolute path (and thus avoid an include path search) without relying on the working directory being the directory in which bootstrap. php resides.

How do you go up a file in PHP?

If you are using PHP 7.0 and above then the best way to navigate from your current directory or file path is to use dirname(). This function will return the parent directory of whatever path we pass to it.

What is __ FILE __ Wordpress?

__FILE__ is simply the name of the current file. realpath(dirname(__FILE__)) gets the name of the directory that the file is in -- in essence, the directory that the app is installed in. And @ is PHP's extremely silly way of suppressing errors.

What does dirname do in PHP?

The dirname() function in PHP is an inbuilt function which is used to return the directory name of a given path. The dirname() function is used to parent directory's path i.e levels up from the current directory.


1 Answers

PHP 5.3+

return dirname(__DIR__); 

PHP 5.2 and lower

return dirname(dirname(__FILE__)); 

With PHP7 go further up the directory tree by specifying the 2nd argument to dirname. Versions prior to 7 will require further nesting of dirname.

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

like image 86
Charles Sprayberry Avatar answered Sep 22 '22 23:09

Charles Sprayberry