Get webroot in PHP

前端 未结 4 904
不知归路
不知归路 2021-01-06 03:03

I am using Apache server for PHP. How can I retrieve my web root in PHP, like http://localhost/testthesis/?

相关标签:
4条回答
  • 2021-01-06 03:28

    Relative paths

    For your webservers root directory, use:

    $folder = '/';
    

    For the directory of the retrieved script, use:

    $folder = './';
    

    Absolute paths (from the client's perspective)

    For your webservers root directory, use:

    $protocol = $_SERVER['HTTPS'] == '' ? 'http://' : 'https://';
    $folder = $protocol . $_SERVER['HTTP_HOST'];
    

    For the directory of the retrieved script, use:

    $protocol = $_SERVER['HTTPS'] == '' ? 'http://' : 'https://';
    $folder = $protocol . $_SERVER['HTTP_HOST'] . '/' . basename($_SERVER['REQUEST_URI']);
    
    0 讨论(0)
  • 2021-01-06 03:29

    Web root is always just /. You'd never need the hostname or protocol part, and root can be only root of the server, not some folder or file.

    If you need some path, like /testthesis/ - there are ways, but it has nothing common with web root.

    If you need a filesystem directory for the webroot - it's in the $_SERVER['DOCUMENT_ROOT'] variable.

    0 讨论(0)
  • 2021-01-06 03:32

    What you're looking for is not a webroot but rather a URL.

    You can get your current URL like so:

    $protocol = strpos($_SERVER['SERVER_SIGNATURE'], '443') !== false ? 'https://' : 'http://';
    $url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    
    0 讨论(0)
  • 2021-01-06 03:45

    Here is one way of doing it:

    $web_root = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/";
    
    OUTPUT -->http://website.com/parent_folder/
    
    0 讨论(0)
提交回复
热议问题