开发者

useing __FILE__ and locating root folder in php ? how do i do this?

lets say we have a directory.

project/index.php

in i开发者_StackOverflow社区ndex.php

<?php
require 'config/config.php';
echo ROOTPATH;
?>

project/config/config.php

<?php
define('ROOTPATH', rtrim(dirname(__FILE__), '\\/'));
?>

it will return like D:\Project Storage\Web Projects\www\project\config.

question:

  1. is there an easy way to get the rootpath without puting it on the project folder ? ( i know this is supid but i cant stand to ask )

  2. is there a way to get the \project\ ( root folder ) ? even if we change the folder location ?

Thanks

Adam Ramadhan

ps* please leave a comment if you dont understand what i mean.


realpath() ITs your friend.. for example:

in config/config.php

define('ROOTPATH', realpath(dirname(__FILE__).'/..'));

Or to get the folder above project:

define('ROOTPATH', realpath(dirname(__FILE__).'/../..'));

I dont work on windows, but if i recall realapath will sort out the slashes for you... if thats not the case then use the DIRECTORY_SEPARATOR constant for compatibility.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜