In PHP how can I get the site folder path anywhere in the site?
ex: /var/www/sitename
this is what I got so far:
$r1 = $_SERVER['document_root'] // = /var/www
$r2 = __DIR__ // = /var/www/sitename/view/folder/subf
$r3 = str_replace($r1 . '/', '', $r2); // = sitename/views/folder/subf
$arr = explode('/', $r3); // = array (sitename, views, folder ...)
$sitepath = $r1 . '/' . $arr[0] // = /var/www/sitename
Is there any other easiest way?.
EDIT: there are many site copies in different servers with different names, could be site1 (/var/www/site1) or sitexx347(/var/www/sitexx347) or onemoresite.com (public_html/onemoresite), and I want to know it anywhere in the website so instead of calling
require_once '../../../../../../config.php';
or
require_once $_SERVER['DOCUMENT_ROOT'] . '/IveNoIdeaWhatTheNameIs/config.php';
I can do this:
require_once $sitepath . 'config.php';
I repeat, I have no idea what the site name is.
EDIT 2: the way I do it now works perfect, I was just trying to know an easiest way. Actually this can be shorten to do it like this:
$r = explode('/', str_replace($_SERVER['DOCUMENT_ROOT'].'/', '', __DIR__));
$sitepath = $_SERVER['DOCUMENT_ROOT'] . '/' . $r[0];