How to get the absolute path to the public_html folder?

后端 未结 11 1348
离开以前
离开以前 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:10

    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."/";
    }
    
    0 讨论(0)
  • 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??

    0 讨论(0)
  • 2020-12-08 15:11
    <?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/
    
    0 讨论(0)
  • 2020-12-08 15:13

    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'
    
    0 讨论(0)
  • 2020-12-08 15:14

    Whenever you want any sort of configuration information you can use phpinfo().

    0 讨论(0)
提交回复
热议问题