php 如何将文件包含在 2 个目录中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/162873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:44:08  来源:igfitidea点击:

How do I include a file over 2 directories back?

phpincludefilesystems

提问by Konrad Rudolph

How do you include a file that is more than 2 directories back. I know you can use ../index.phpto include a file that is 2 directories back, but how do you do it for 3 directories back? Does this make sense? I tried .../index.phpbut it isn't working.

你如何包含一个超过 2 个目录的文件。我知道您可以使用../index.php包含 2 个目录的文件,但是您如何为 3 个目录执行此操作?这有意义吗?我试过了,.../index.php但它不起作用。

I have a file in /game/forum/files/index.phpand it uses PHP include to include a file. Which is located in /includes/boot.inc.php; /being the root directory.

我有一个文件,/game/forum/files/index.php它使用 PHP 包含来包含一个文件。其中位于/includes/boot.inc.php; /作为根目录。

回答by Konrad Rudolph

..selects the parent directory from the current. Of course, this can be chained:

..从当前选择父目录。当然,这可以链接:

../../index.php

This would be two directories up.

这将是两个目录。

回答by Dan Hulton

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

要将文件包含在一个目录后,请使用'../file'. 对于后面的两个目录,使用'../../file'. 等等。

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_ROOTis a server variable that represents the base directory that your code is located within.

DOCUMENT_ROOT是一个服务器变量,表示您的代码所在的基本目录。

回答by Joe Skora

. = 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.

../多次必要去了2倍或以上的水平。

回答by gaborous

include dirname(__FILE__).'/../../index.php';

is your best bet here, and it will avoid most of the relative path bugs you can encounter with other solutions.

是您最好的选择,它将避免您在使用其他解决方案时可能遇到的大多数相对路径错误。

Indeed, it will force the include to always be relative to the position of the current scriptwhere this code is placed (which location is most likely stable, since you define the architecture of your application). This is different from just doing include '../../index.php'which will include relatively to the executing (also named "calling") script and then relatively to the current working directory, which will point to the parent script that includes your script, instead of resolving from your included script's path.

实际上,它会强制包含始终相对于放置此代码的当前脚本的位置(哪个位置最有可能是稳定的,因为您定义了应用程序的架构)。这与仅include '../../index.php'包含相对于执行(也称为“调用”)脚本然后相对于当前工作目录的操作不同,后者将指向包含您的脚本的父脚本,而不是从您包含的脚本的路径解析.

From the PHP documentation:

来自 PHP 文档:

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing.

根据给定的文件路径包含文件,如果没有给出,则指定 include_path。如果在 include_path 中找不到该文件,include 将在失败之前最终检查调用脚本自己的目录和当前工作目录。

And the oldest post I've found citing this trick dates back to 2003, by Tapken.

我发现引用这个技巧的最古老的帖子可以追溯到 2003 年,由 Tapken 撰写

You can test with the following setup:

您可以使用以下设置进行测试:

Create a layout like this:

创建这样的布局:

htdocs
|   parent.php
|   goal.php
|
+---sub
    |   included.php
    |   goal.php

In parent.php, put:

parent.php,输入:

<?php
include dirname(__FILE__).'/sub/included.php';
?>

In sub/included.php, put:

sub/included.php,输入:

<?php
print("WRONG : " . realpath('goal.php'));
print("GOOD : " . realpath(dirname(__FILE__).'/goal.php'));
?>

Result when accessing parent.php:

访问时的结果parent.php

WRONG : X:\htdocs\goal.php
GOOD : X:\htdocs\sub\goal.php

As we can see, in the first case, the path is resolved from the calling script parent.php, while by using the dirname(__FILE__).'/path'trick, the include is done from the script included.phpwhere the code is placed in.

正如我们所看到的,在第一种情况下,路径是从调用脚本中解析出来的parent.php,而通过使用这个dirname(__FILE__).'/path'技巧,包含是从included.php放置代码的脚本中完成的。

Beware, the following NOT equivalent to the trick above contrary to what can be read elsewhere:

请注意,以下不等同于上述技巧与其他地方可以阅读的内容相反:

include '/../../index.php';

Indeed, prepending /will work, but it will resolve just like include ../../index.phpfrom the calling script (the difference is that include_pathwon't be looked afterwards if it fails). From PHP doc:

事实上,前置/会起作用,但它会像include ../../index.php从调用脚本中一样解决(不同之处在于include_path如果失败则不会在之后查看)。来自 PHP 文档

If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether.

如果定义了路径——无论是绝对路径(在 Windows 上以驱动器号或 \ 开头,或在 Unix/Linux 系统上以 / 开头)还是相对于当前目录(以 . 或 .. 开头)——include_path 将被完全忽略。

回答by Brian Dillingham

../is one directory, Repeat for two directories ../../or even three: ../../../and so on.

../是一个目录,重复两个目录../../甚至三个:../../../等等。

Defining constants may reduce confusion because you will drill forward into directories verses backwards

定义常量可能会减少混淆,因为您将向前钻取目录和向后

You could define some constants like so:

您可以像这样定义一些常量:

define('BD', '/home/user/public_html/example/');

define('HTMLBD', 'http://example.com/');

When using 'BD' or my 'base directory' it looks like so:

使用“BD”或我的“基本目录”时,它看起来像这样:

file(BD.'location/of/file.php');

define(); reference

定义(); 参考

回答by bdukes

../../index.php 

       

       

回答by leek

../../../includes/boot.inc.php

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

每个实例../表示向上/返回一个目录。

回答by user2951753

following are ways to access your different directories:-

以下是访问不同目录的方法:-

./ = Your current directory
../ = One directory lower
../../ = Two directories lower
../../../ = Three directories lower

回答by ólafur Waage

You can do ../../directory/file.txt- This goes two directories back.

你可以这样做../../directory/file.txt- 这会返回两个目录。

../../../- this goes three. etc

../../../- 这是三个。等等

回答by MazarD

../../../index.php