How do I include a file over 2 directories back?

后端 未结 19 1454
遥遥无期
遥遥无期 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:55
    ../../../includes/boot.inc.php
    

    Each instance of ../ means up/back one directory.

    0 讨论(0)
  • 2020-12-22 17:55

    Try This

    this example is one directory back

    require_once('../images/yourimg.png');
    

    this example is two directory back

    require_once('../../images/yourimg.png');
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-22 17:59

    if you include the / at the start of the include, the include will be taken as the path from the root of the site.

    if your site is http://www.example.com/game/forum/files/index.php you can add an include to /includes/boot.inc.php which would resolve to http://www.example.com/includes/boot.inc.php .

    You have to be careful with .. traversal as some web servers have it disabled; it also causes problems when you want to move your site to a new machine/host and the structure is a little different.

    0 讨论(0)
  • 2020-12-22 18:02

    following are ways to access your different directories:-

    ./ = Your current directory
    ../ = One directory lower
    ../../ = Two directories lower
    ../../../ = Three directories lower
    
    0 讨论(0)
  • 2020-12-22 18:05
    . = current directory
    .. = parent directory
    

    So ../ gets you one directory back not two.

    Chain ../ as many times as necessary to go up 2 or more levels.

    0 讨论(0)
提交回复
热议问题