How to use dirname(__FILE__) in php to get the absolute path of the current file

伊谢尔伦
Release: 2023-03-11 22:38:02
Original
2422 people have browsed it

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:

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

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 file require or include in 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 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 one. (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

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 an include 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:

<?php 
echo FILE ; // 取得当前文件的绝对地址,结果:D:\www\test.php 
echo dirname(FILE); // 取得当前文件所在的绝对目录,结果:D:\www\ 
echo dirname(dirname(FILE)); //取得当前文件的上一层目录名,结果:D:\ 
?>
Copy after login

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!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!