$_SERVER[\'DOCUMENT_ROOT\']
returns
/usr/local/apache/htdocs/
is there a way to get
/home/us
You just need to create an offset bypass to how far you want the backwards reading to go. So, we use getcwd()
to get the path and explode (split into array) to fetch the data between $root
and the ending of the path.
function getRoot($root = "public_html") {
return explode($root, getcwd())[0].$root."/";
}
put anyfile on the directories you wanted to find, in this case, place 'root' at public_html
/home/user/public_html/root <- note that 'root' is not a folder (you can use root.txt if u want)
And use this function
function findThis($get){
$d = '';
for($i = 0; $i < 20; $i++){//this will try 20 times recursively on upper folder
if(file_exists($d.$get)){
return $d;
}else{
$d.="../";
}
}
}
and get the value by calling it
$pathToRoot = findThis('root');
And it will return, for example the the dir of php script is
/home/user/public_html/test/another-dir/test.php
so the $pathToRoot will be
$pathToRoot => "../../../"
Is this the one you want??
<?php
// Get absolute path
$path = getcwd(); // /home/user/public_html/test/test.php.
$path = substr($path, 0, strpos($path, "public_html"));
$root = $path . "public_html/";
echo $root; // This will output /home/user/public_html/
Let's asume that show_images.php
is in folder images
and the site root is public_html
, so:
echo dirname(__DIR__); // prints '/home/public_html/'
echo dirname(__FILE__); // prints '/home/public_html/images'
Whenever you want any sort of configuration information you can use phpinfo().