Home > Article > Backend Development > PHP gets the file path (the wonderful use of __FILE__)
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. |