Get relative path to a parent directory

后端 未结 2 754
梦毁少年i
梦毁少年i 2021-01-24 05:41

I have a scenario where I want to get a path back to a specific parent directory. Here is an example folder strcuture ([something] is a folder)

inde         


        
相关标签:
2条回答
  • 2021-01-24 05:57

    You can use .. to get into the parent directory:

    <?php
        // somePage.php
        include('../components/header.php');
    ?>
    
    0 讨论(0)
  • 2021-01-24 05:59

    I would make sure that you always know the root of your site so that you can take it from there.

    To do that, you could go at least two ways:

    • Include a config file that defines your site-root and prepend that root to every include like (in case of a constant):
      include MY_SITE_ROOT . '/path/to/file.php';
    • Use a server variable to achieve the same like:
      include $_SERVER['DOCUMENT_ROOT'] . '/path/to/file.php';
    0 讨论(0)
提交回复
热议问题