root path doesn't work with php include

后端 未结 8 1585
天命终不由人
天命终不由人 2020-12-03 00:56

/ in the beginning of a link to get to the root folder doesn\'t work in php include.

for example \"/example/example.php\"

What is the solution?

8条回答
  •  暖寄归人
    2020-12-03 01:06

    some versions of PHP may have the delimiter at the end of document root while others may not. As a practical matter you may want to use:

        $r = trim(filter_input(INPUT_SERVER, 'DOCUMENT_ROOT', FILTER_SANITIZE_STRING));
        if (substr($r, 0, 1) == '/')
        {
          define("PATCH_SEPARATOR", "/");  
        }
        else
        {
          define("PATCH_SEPARATOR", "\\");    
        }
    
        if (substr($r, -1) == PATCH_SEPARATOR)
        {
          include_once ($r . 'example/example.php');
        }
        else
        {
          include_once ($r . PATCH_SEPARATOR . 'example/example.php');
        }
    

提交回复
热议问题