Home >Backend Development >PHP Tutorial >Detailed explanation of dirname(__FILE__) use cases
This time I will bring you a detailed explanation of the use case of dirname (FILE). What are the precautions when using dirname (FILE). The following is a practical case, let's take a look.
FILE represents the absolute path of the current file including the file name, dirname(FILE) represents the absolute path of the current file, basename(FILE) represents the file name of the current file, dirname(FILE) ."/f/".basename(FILE) means a file with the file name dirname(FILE) in the f directory in the directory where the current file is located. require means including the file into this file.
The following is a detailed explanation:
1) The dirname(FILE_) function returns the path where the script is located.
For example, the file b.php contains the following content:
<?php $basedir = dirname(FILE); ?>
If b.php is referenced by the a.php file require or include in other directories.
The content of the variable $basedir is still the path to the folder where b.php is located.
Instead of changing to the directory where the a.php file is located.
2) dirname(FILE) will generally return a directory structure from 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 a . (current directory)
[The reason is that the b.php file is in the default WEB directory of http.conf or PHP Configuring the development environment.
For example, WEB_ROOT is: "C:/root/www/".]
b.php file path is: "C:/root/www/b.php".
3) Use Method tip,
If you repeat it once, you can move the directory up a level:
For example:
$d = dirname(dirname(FILE));
In fact, you give a directory as a parameter to dirname() Yes. Because dirname() returns the last directory without \\ or /
, when it is used repeatedly, it can be considered that dirname() treats the lowest directory as a file name. Return
to the upper-level directory of the current directory as usual. Repeat this to get its upper-level directory.
4) Contains files that get the upper-level directory
include(dirname(FILE).''/../filename
The difference between dirname(FILE) and dirname(dirname(FILE)) in php
dirname(dirname(FILE));
Assume FILE is /home/web/config/config.php
The output of the above method is /home/web
dirname(dirname(FILE)); What is obtained is the directory name of the layer above the file
dirname(FILE); What is obtained is the layer where the file is located Directory name
Script House editor’s note: In fact, PHP’s dirname() function and FILE
Definition and usage
dirname() function return The directory portion of the path.
Syntax
dirname(path)
Parameters | Description |
---|---|
path | Required. Specifies the path to be checked. |
Description
The path parameter is a string containing the full path to a file. This function returns the directory name after removing the file name.
Example
<?php echo dirname("c:/testweb/home.php"); echo dirname("/testweb/home.php"); ?>
Output:
c:/testweb
/testweb
I believe you have read the case in this article After mastering the method, please pay attention to other related articles on the php Chinese website for more exciting content!
Recommended reading:
Laravel ORM’s detailed explanation of the Model::find cache method
thinkPHP controller variable’s detailed explanation of the steps to display in the template
The above is the detailed content of Detailed explanation of dirname(__FILE__) use cases. For more information, please follow other related articles on the PHP Chinese website!