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:
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 )
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.
精彩评论