Home  >  Article  >  Backend Development  >  PHP gets the file path (the wonderful use of __FILE__)

PHP gets the file path (the wonderful use of __FILE__)

WBOY
WBOYOriginal
2016-07-25 09:00:041361browse
In PHP programming, operations related to file paths are still common. How to obtain the file path? There are many methods. This article introduces the usage of __FILE__, which can easily obtain the file path for your reference.

__FILE__ , is the full path and file name of the returned 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. dirname(__FILE__), returns the partial path of the current file path, that is, removing the file name.

Example:

<?php
// 获取文件的当前路径 + 文件名
echo __FILE__;
echo '<BR>';
// 获取文件的当前路径
echo dirname(__FILE__);
echo '<BR>';
// 获取文件的上一级目录路径
echo dirname(dirname(__FILE__));
//by http://bbs.it-home.org
?>

Run results: D:wampwwwindex.php D:wampwww D:wamp

__FILE__ is very useful. It can get the current path of the file. It will not change even if it is included. It can be used as the absolute path of the website on the server, and other directories will be distributed based on this.



Statement:
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