How do I include a file over 2 directories back?

后端 未结 19 1584
遥遥无期
遥遥无期 2020-12-22 17:51

How do you include a file that is more than 2 directories back. I know you can use ../index.php to include a file that is 2 directories back, but how do you do

19条回答
  •  天涯浪人
    2020-12-22 17:59

    To include a file one directory back, use '../file'. For two directories back, use '../../file'. And so on.

    Although, realistically you shouldn't be performing includes relative to the current directory. What if you wanted to move that file? All of the links would break. A way to ensure that you can still link to other files, while retaining those links if you move your file, is:

    require_once($_SERVER['DOCUMENT_ROOT'] . 'directory/directory/file');
    

    DOCUMENT_ROOT is a server variable that represents the base directory that your code is located within.

提交回复
热议问题