php root directory code example
Example 1: php document root
<?php echo $_SERVER['DOCUMENT_ROOT'].'/hello.html'; ?>
Example 2: php root path
$_SERVER['DOCUMENT_ROOT']
Example 3: root directory in php
function getHtmlRootFolder(string $root = '/var/www/') {
$ret = str_replace(' ', '', $_SERVER['DOCUMENT_ROOT']);
$ret = rtrim($ret, '/') . '/';
if (!preg_match("#".$root."#", $ret)) {
$root = rtrim($root, '/') . '/';
$root_arr = explode("/", $root);
$pwd_arr = explode("/", getcwd());
$ret = $root . $pwd_arr[count($root_arr) - 1];
}
return (preg_match("#".$root."#", $ret)) ? rtrim($ret, '/') . '/' : null;
}