root path doesn't work with php include

后端 未结 8 1574
天命终不由人
天命终不由人 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:19

    maybe it's a bit unconventional

    If I have a case like

    /var/www/namedir/  <= root
    
    /var/www/namedir/example/example.php <= file to include
    
    -- directory when i need the include -- 
    /var/www/namedir/dir1/page.php 
    /var/www/namedir/dir1/dirA/page.php
    /var/www/namedir/dir1/dirB/page.php
    

    the solution that I use is simple. get the path before the "Dir1"

    something like this

    include (substr(dirname(__FILE__),0,strpos(dirname(__FILE__), '/dir1'))."/example/example.php");
    

    I found it usefull id i need to rename the main subdir for example from

    /var/www/namesite/internalDirA/dirToInclude/example.php  
    /var/www/namesite/internalDirA/dir1/dirA/example.php 
    /var/www/namesite/internalDirA/dir1/dirB/example.php 
    

    TO

    /var/www/namesite/dirReserved/dirToInclude/example.php  
    /var/www/namesite/dirReserved/dir1/dirA/example.php 
    /var/www/namesite/dirReserved/dir1/dirB/example.php 
    

提交回复
热议问题