$thisFile = str_replace('\\', '/', __FILE__);
$docRoot = $_SERVER['DOCUMENT_ROOT'];
$webRoot = str_replace(array($docRoot, 'library/config.php'), '', $thisFile);
$srvRoot = str_replace('library/config.php', '', $thisFile);
This is how I get the web root and the server root
web root is the folder name where my files belongs, server root is the C:/xampp/htdocs/folder_name
I just want to ask when should I use webroot and server root.
I just notice that if you are referencing a file ex: href or img src, then only web root works but not server root
But, if you require a file, ex. require_once 'file.php'; only server root works not webroot
You are confusing the server path with the URL path.
On your server, the files are stored at
C:/xampp/htdocs/folder_name, but according to your web browser, they are athttp://localhost/folder_name. When your browser loads the files, it doesn’t know they are inC:/xampp/htdocs/.NOTE: None of the code you have there will get you the “web root”.
__FILE__will return the path of the file on your server, like:`C:/xampp/htdocs/include/header.php.$_SERVER['DOCUMENT_ROOT']will return you the path on the server where the files are, like:C:/xampp/htdocs/.To get your “web root”, you need to use
$_SERVER['SERVER_NAME']. This will return youexample.com(or whatever). You’re probably gonna need to appendhttp(s)://to it.You can also just use
/as your web root.and
should be equivalent.
Try to
var_dump($_SERVER);, it’ll give you some fun information.EDIT: Use
dirnameif you want to get the “sub folder” under the “root folder”. The above will give you the main root.