这是snippit
$thisFile = str_replace('\\', '/', __FILE__);
$docRoot = $_SERVER['DOCUMENT_ROOT'];
$webRoot = str_replace(array($docRoot, 'library/config.php'), '', $thisFile);
$srvRoot = str_replace('library/config.php', '', $thisFile);
如果我将它作为文件保存到test.php中。我很困惑的部分是str_replace如何使$ webRoot值出来test.php
答案 0 :(得分:2)
该代码将计算绝对文件系统路径($srvRoot
)和绝对URL路径($webRoot
)到应用程序目录,我想,从那里可以使用{{1 }}
__FILE__
是一个魔术常量,包含使用常量的文件的绝对文件系统路径。library/config.php
将包含反斜杠替换反斜杠的文件系统路径。 $thisFile
是可从Web访问的根目录的绝对文件系统路径。
因此,例如,如果$docRoot
为__FILE__
且/var/www/htdocs/apps/foobar/library/config.php
为$_SERVER['DOCUMENT_ROOT']
,/var/www/htdocs
为$webRoot
且/apps/foobar/
是$srvRoot
。
答案 1 :(得分:1)
变量__FILE__
是当前源文件的名称,或“test.php”。