Home > Backend Development > PHP Tutorial > Application of php dirname(__FILE__) and __FILE__ constant_PHP tutorial

Application of php dirname(__FILE__) and __FILE__ constant_PHP tutorial

WBOY
Release: 2016-07-20 10:59:05
Original
931 people have browsed it

In php, dirname() and __FILE__ are our constants. This article will give you a detailed introduction to some matters in the use of dirname() and __FILE__ constants. Friends who are interested can refer to them. ​

dirname() function

string dirname ( string $path )

Given a string containing the full path to a file, this function returns the directory name after removing the file name.

In Windows, both slash (/) and backslash () can be used as directory separators. In other circumstances it is a slash (/).


Example #1 dirname() Example

Example

The code is as follows Copy code
 代码如下 复制代码

echo dirname("c:/testweb/home.php");
echo dirname("/testweb/home.php");
?>

输出:

c:/testweb
/testweb

echo dirname("c:/testweb/home.php");

echo dirname("/testweb/home.php"); ?>

Output:

c:/testweb

/testweb



__FILE__
 代码如下 复制代码

$basedir = dirname(__FILE__);
echo $basedir
//将在页面打印出一个这个文件所在绝对路径!
?>

The full path and file name of the file. If used in an included file, returns the included file name

dirname(__file__)

__FILE__: Known as PHP magic 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. Update network

For example, the file b.php contains the following content:

The code is as follows Copy code

$basedir = dirname(__FILE__);
echo $basedir //The absolute path where this file is located will be printed out on the page!

?>


The test I did got the result: E:websiteothertestcms

dirname(__FILE__) generally returns 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.

 代码如下 复制代码
if($reguser != ""){
echo"Submit Photo
";
echo"Log Out";
}else{
echo"Login";
}
?>
dirname(__FILE__) may also return a . (current directory)
[The reason is that the b.php file is in http.conf or the default WEB directory of the PHP configuration development environment. For example, WEB_ROOT is: "C:/root/www/".]

The path of the b.php file is: "C:/root/www/b.php". Usage tips, if you repeat it, you can move the directory up a level: In fact, you just give a directory as a parameter to dirname(). Because dirname() returns the last directory without \\ or / Therefore, when reused, it can be considered that dirname() treats the lowest directory as a file name. Return as usual The upper-level directory of the current directory. Repeat this to get its upper-level directory. Include the file that gets the upper-level directory include(dirname(__FILE__).'/../filename.php'); 2. How to combine PHP files with html files
The code is as follows Copy code
if($reguser != "" ){ echo"Submit Photo "; echo "Log Out"; }else{ echo"Login"; } ?>
http://www.bkjia.com/PHPjc/445633.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445633.htmlTechArticleIn php, dirname() and __FILE__ are our constants. This article will introduce dirname() to you in detail. Interested friends can refer to some matters related to the use of __FILE__ constant. dirn...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template