PHP include absolute path

后端 未结 3 2008
遇见更好的自我
遇见更好的自我 2020-12-01 14:11

I have a variable on my site called $basePath which is set as:

$basePath = \'/Systems/dgw/\';

I am using it on all my css, js and images ta

3条回答
  •  有刺的猬
    2020-12-01 15:10

    If your server doesn't populate the "document_root", you may need this



        require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/path/to/file.php");
    

    I use this line of code. It goes back to the "top" of the site tree, then goes to the file desired.
    For example, let's say i have this file tree:
    domain.com/aaa/index.php
    domain.com/bbb/ccc/ffffd/index.php
    domain.com/_resources/functions.php

    I can include the functions.php file from wherever i am, just by copy pasting

    require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/_resources/functions.php");
    



    If you need to use this code many times, you may create a function that returns the "str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))" part. Then just insert this function in the first file you include. I have an "initialize.php" file that i include at the very top of each php page and which contains this function. The next time i have to include files, i in fact just use the function (named "path_back"):

    require(path_back()."/_resources/another_php_file.php");
    

提交回复
热议问题