How do I set an absolute include path in PHP?

后端 未结 10 1912
别那么骄傲
别那么骄傲 2020-12-04 11:32

In HTML, I can find a file starting from the web server\'s root folder by beginning the filepath with "/". Like:

/images/some_image         


        
10条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 11:48

    I've come up with a single line of code to set at top of my every php script as to compensate:

    2;$i--) $root .= "../"; ?>
    

    By this building $root to bee "../" steps up in hierarchy from wherever the file is placed. Whenever I want to include with an absolut path the line will be:

    
    

    I don't really like it, seems as an awkward way to solve it, but it seem to work whatever system php runs on and wherever the file is placed, making it system independent. To reach files outside the web directory add some more ../ after $root, e.g. $root."../external/file.txt".

提交回复
热议问题