How to get the absolute path to the public_html folder?

后端 未结 11 1349
离开以前
离开以前 2020-12-08 14:25
$_SERVER[\'DOCUMENT_ROOT\']

returns

/usr/local/apache/htdocs/

is there a way to get

/home/us         


        
11条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 15:11

    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??

提交回复
热议问题