PHP absolute path to root

后端 未结 7 1911
轻奢々
轻奢々 2020-11-28 05:44

I can\'t believe PHP doesn\'t have an easy solution for this simple matter. ASP.NET has a ~ sign that takes care of this issue and starts everything from the root level. Her

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 06:16

    Create a constant with absolute path to the root by using define in ShowInfo.php:

    define('ROOTPATH', __DIR__);
    

    Or PHP <= 5.3

    define('ROOTPATH', dirname(__FILE__));
    

    Now use it:

    if (file_exists(ROOTPATH.'/Texts/MyInfo.txt')) {
      // ...
    }
    

    Or use the DOCUMENT_ROOT defined in $_SERVER:

    if (file_exists($_SERVER['DOCUMENT_ROOT'].'/Texts/MyInfo.txt')) {
      // ...
    }
    

提交回复
热议问题