PHP 获取文件路径(灵活应用__FILE__)_PHP

WBOY
Release: 2016-06-01 12:08:43
Original
1338 people have browsed it

__FILE__ ,是返回文件的完整路径和文件名。如果用在包含文件中,则返回包含文件名。自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路径,而在此之前的版本有时会包含一个相对路径。dirname(__FILE__),返回当前文件路径的部分路径,也就是去掉文件名。

演示代码:

复制代码 代码如下:
 // 获取文件的当前路径 + 文件名
 echo __FILE__;
 echo '
';
 // 获取文件的当前路径
 echo dirname(__FILE__);
 echo '
';
 // 获取文件的上一级目录路径
 echo dirname(dirname(__FILE__));
?>

运行结果:

复制代码 代码如下:
D:\wamp\www\index.php
D:\wamp\www
D:\wamp

__FILE__很好使,可以获取文件当前的路径,即便被include也不会改变,可以作为网站在服务器上的绝对路径,其他目录以此为基准分布。

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 [email protected]
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!