Definition and usage
The DirName () function returns the directory portion of the path.
Grammar
DirName (path) parameter description
Path is required. Specifies the path to check.
Description
The path parameter is a string that contains a full path to a file. This function returns the name of the directory after the file name has been removed.
Example
Echo dirname ("c:/testweb/home.php");
Echo dirname ("/testweb/home.php");
?>
Output:
C:/testweb
/testweb
A useful constant is defined in PHP, which is
__file__
The default constant is the full path (path + filename) of the current PHP program.
Even if the file is referenced by another file (include or require), __file__ is always the full path to the file it resides in, not the full path to the file that references it.
Take a look at the following example:
/home/data/demo/test/a.php
Echo dirname ("c:/testweb/home.php");
Echo dirname ("/testweb/home.php");
?>
home/data/demo/b.php
Echo dirname ("c:/testweb/home.php");
Echo dirname ("/testweb/home.php");
?>
To put it simply:
__FILE__ returns the current path + file name
DirName (__file__) returns the path portion of the current file path
DirName (DirName (__file__)); Gets the file on the top of the directory name (does not include the last "/" number)
For example, if the current file is/home/data/demo/test.php, the
__FILE__ get is the full path is/home/data/demo/test.php, and
DirName (__file__) Gets the path part that is the/home/data/demo (no "/" number behind)