Home  >  Article  >  php教程  >  PHP路径相关 dirname,realpath,__FILE__,dirnamerealpath

PHP路径相关 dirname,realpath,__FILE__,dirnamerealpath

WBOY
WBOYOriginal
2016-06-13 08:44:50996browse

PHP路径相关 dirname,realpath,__FILE__,dirnamerealpath

​比如:程序根目录在:E:\wamp\www 中

 

1.    __FILE__   当前文件的绝对路径

如果在index.php中调用 则返回  E:\wamp\www\index.php

 

下面再看一下 程序根目录的目录结构

 

 

如果在 c_system_base.php中调用__FILE__ 则返回:

E:\wamp\www\zb_system\function\c_system_base.php

 

2.dirname    返回当前目录的上级目录 或当前文件所在的目录 (结尾不带/)

一般跟__FILE__联合使用

 

如果在 c_system_base.php中调用dirname(__FILE__) 则返回 

E:\wamp\www\zb_system\function    (文说件返回所在目录)

如果调用 dirname(dirname(__FILE__)) 则返回  

E:\wamp\www\zb_system        (目录返回上级目录)

 

realpath()    方法 返回当前文件的绝对路径 或 相对根目录的路径

如果在 c_system_base.php中调用realpath(__FILE__) 则返回

E:\wamp\www\zb_system\function\c_system_base.php

 

realpath('/')    返回磁盘根目录    E:\

realpath('./')    返回根目录  E:\wamp\www

realpath('../')    返回相对程序根目录的上级目录

也可以和dirname配合使用 一般还会加上几个相对路径../ (前面要加/  即:/../)

realpath(dirname(__FILE__))    返回  E:\wamp\www\zb_system\function

realpath(dirname(__FILE__).'/../')  返回 E:\wamp\www\zb_system

realpath(dirname(__FILE__).'/../../')    返回    E:\wamp\www

 

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