calling include from an included file

后端 未结 3 444
攒了一身酷
攒了一身酷 2020-12-14 16:45

So, examining this directory structure

  • /include_one.php
  • /include_two.php
  • /directory/main_file.php

A

3条回答
  •  心在旅途
    2020-12-14 17:02

    The "relative include path" is not shifted to the included file... Which means that using relative paths generally ends badly.

    A better solution, that I use almost all the time, is to always work with absolute paths -- and you can mix relatives and absolute paths using __DIR__, to get the directory that contains the file where this is written.


    For example, in include_one.php, you'd use :

    require_once __DIR__ . '/include_two.php';
    

    To include the include_two.php file that's in the same directory as include_one.php.


    And, in main_file.php, you'd use :

    require_once __DIR__ . '/../include_one.php';
    

    To include the include_one.php file that's one directory up.


    With that, your includes will work, no matter from which file they are called.

提交回复
热议问题