How to programmatically determine the document root in PHP?

后端 未结 6 1828
猫巷女王i
猫巷女王i 2020-12-15 13:28

Here\'s a problem that I\'ve been running into lately - a misconfigured apache on a webhost. This means that all scripts that rely on $_SERVER[\'DOCUMENT_ROOT\']

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 14:04

    Based on http://www.helicron.net/php/:

    $localpath=getenv("SCRIPT_NAME");
    $absolutepath=getenv("SCRIPT_FILENAME");
    $_SERVER['DOCUMENT_ROOT']=substr($absolutepath,0,strpos($absolutepath,$localpath));     
    

    I had to change the basename/realpath trick because it returned an empty string on my host. Instead, I use SCRIPT_FILENAME. This probably won't work on IIS anymore (but the original scripts that used the $_SERVER variable probably wouldn't either).

提交回复
热议问题