The meaning and application of dirname of __FILE__

  • 2021-09-16 06:31:49
  • OfStack

__FILE__ denotes the absolute path of the current file including the file name, dirname (__FILE__) denotes the absolute path of the current file, basename (__FILE__) denotes the file name of the current file, dirname (__FILE__). "/f/". basename (__FILE__) denotes the file named dirname (__FILE__) in the f directory under the current file's directory, and require denotes including the file to 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:


<?php
$basedir = dirname(__FILE__);
?>

If b. php is referenced by the a. php file require or include in another directory.
The contents of the variable $basedir are also the path to the folder where b. php is located.
Instead of becoming the directory where the a. php files are located.

2) dirname (__FILE__) 1 returns a directory structure from the current directory of the file to the root of the system.

The current file name is not returned.
dirname (__FILE__) may also return 1. (Current directory)
[The reason is that the b. php file is in the default WEB directory of the http. conf or PHP configuration development environment.
For example, WEB_ROOT is: "C:/root/www/".]
The file path of b. php is: "C:/root/www/b. php".

3) Use method prompt,

If you repeat it once, you can upgrade the directory by one level:

For example: $d = dirname(dirname(__FILE__));

In fact, it is to give a directory to dirname () as a parameter. Because the last directory returned by dirname () does not take\\ or is/
So when you reuse it, you can think that dirname () treats the lowest directory as a file name. Return as usual
The parent directory of the current directory. This repetition will get its upper level 1 directory.

4) Include files that get the top level 1 directory

include(dirname(__FILE__).''/../filename

Difference between dirname (__FILE__) and dirname (dirname (__FILE__)) of php

dirname(dirname(__FILE__));
Assume __FILE__ is/home/web/config/config. php
The above method output is/home/web

dirname (dirname (__FILE__)); What you get is the Layer 1 directory name on the file

dirname (__FILE__); What you get is the directory name of the layer where the file is located

Note of this site: In fact, the dirname () function of PHP and __FILE__ are used

Definition and usage
The dirname () function returns the directory portion of the path.

Grammar

dirname(path)

参数 描述
path 必需。规定要检查的路径。

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


Related articles: