【发布时间】:2013-03-21 18:13:25
【问题描述】:
当定义位于子目录中时,获取实际(完整)根路径的可靠方法?考虑:
E:\web\sites\name\index.php
E:\web\sites\name\res\defines.php
E:\web\sites\name\res\another.php
E:\web\sites\name\classes\class-example.php
E:\web\ 是本地主机,E:\web\sites\name 是网站的预期根目录。
以$file = ROOTPATH."\data\feature\file.txt";的方式定义=E:\web\sites\name\data\feature\file.txt的示例路径
我不使用相对路径的原因是因为我想在classes\class-example.php 和res\another.php 中传递路径,而不对同一个目标使用两个常量。
那么,在 res\defines.php 中,有什么方法可以让根路径指向 E:\web\sites\name\?当网站在服务器上时,同一个ROOTPATH应该指向http://www.example.com/
这是我在 define.php 位于 res\defines.php 时得到的结果:
// Outputs: E:\web\sites\name\res
echo __DIR__;
// Outputs: E:\web\sites\name\res
echo realpath(__DIR__);
// Outputs: sites\name\res\another.php
echo $_SERVER['REQUEST_URI']
// Outputs: E:\web
echo $_SERVER['DOCUMENT_ROOT'];
// Outputs: sites\name\res\another.php
echo $_SERVER['SCRIPT_NAME'];
// Outputs: E:\web\sites\name\res\defines.php
echo __FILE__;
// Outputs: sites\name\res\another.php
echo $_SERVER['PHP_SELF'];
【问题讨论】: