dirname(FILE) obtains the absolute path of the current file, which means that the search speed is the fastest compared to the relative path.
PHP constant dirname(file)
FILE: called PHPmagic constant, returns the full path and file name of the currently executing PHP script, including an absolute path
1) The dirname(FILE) function returns the path where the script is located.
For example, the file b.php contains the following content:
The test I did gave the result: E:/website/othertest/cms
This is equivalent to, in asp Usage of server.mappth
If b.php is referenced by a.php filerequireorincludein another directory. The content of the variable $basedir is still the path to the folder where b.php is located. Instead of becoming the directory where the a.php file is located.
2) dirname(FILE) generally returns adirectory structurefrom the current directory where the file is located to the system root directory.
The current file name will not be returned. dirname(FILE) may also return one. (current directory) [The reason is that the b.php file is in the default WEB directory of http.conf or PHPConfiguring the development environment
A PHP "magic constant"
Name Description
LINE The current line number in the file.
FILE The full path and file name of the file. If used in aninclude file, returns the include file name. As of PHP 4.0.2, FILE always contains an absolute path, while versions before that sometimes contained a relative path.
FUNCTION Function name (newly added in PHP 4.3.0). Since PHP 5 this constant returns the name of the function as it was defined (case sensitive). In PHP 4 this value is always lowercase.
CLASS The name of the class (new in PHP 4.3.0). Since PHP 5 this constant returns the name of the class when it was defined (case sensitive). In PHP 4 this value is always lowercase.
METHOD class method name (newly added in PHP 5.0.0). Returns the name when the method was defined (case sensitive. For example, the current file is placed under (d:\www\), and the file name is test.php.
The test code is as follows:
Usage tips,
dirname(FILE) obtains the absolute path of the current file, which means that the search speed is the fastest compared to the relative path.
If it is repeated, it can be retrieved. The directory goes up a level:
For example: $d = dirname(dirname(FILE));
In fact, a directory is given as a parameter to dirname(), because dirname() returns the last directory without \. \ or /
So when it is used repeatedly, it can be considered that dirname() treats the lowest directory as a file name. It returns the superior directory of the
current directory as usual. In this way, it is obtained by repeating it. Directory.
Contains files that get the upper level directory
include(dirname(FILE).'/../filename.php');
The path of FILE is the current code The file where it is located
dirname(dirname(FILE)); what you get is the name of the directory above the file
dirname(FILE); what you get is the name of the directory where the file is located
The above is the detailed content of How to use dirname(__FILE__) in php to get the absolute path of the current file. For more information, please follow other related articles on the PHP Chinese website!