php 为什么要在 include 或 include_once 语句中使用 dirname(__FILE__) ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9628443/
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
Why would I use dirname(__FILE__) in an include or include_once statement?
提问by Cheeso
I have seen this:
我见过这个:
<?php
include( dirname(__FILE__) . DIRECTORY_SEPARATOR . 'my_file.php');
?>
Why would I ever need to do this?Why would I go to the trouble of getting the dirname and then concatenating that with a directory separator, and a new filename?
为什么我需要这样做?为什么我要麻烦获取目录名,然后将其与目录分隔符和新文件名连接起来?
Is the code above not equivalent to this:
上面的代码不等同于这个:
<?php
include( 'my_file.php' );
?>
??
??
The PHP docsays,
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 directoryand the current working directory before failing. The include() construct will emit a warning if it cannot find a file; this is different behavior from require(), which will emit a fatal error.
根据给定的文件路径包含文件,如果没有给出,则指定 include_path。如果在 include_path 中没有找到该文件,include() 将在失败之前最终检查调用脚本自己的目录和当前工作目录。如果找不到文件,include() 构造将发出警告;这与 require() 的行为不同,后者会发出致命错误。
回答by Corbin
Let's say I have a (fake) directory structure like:
假设我有一个(假)目录结构,如:
.../root/
/app
bootstrap.php
/scripts
something/
somescript.php
/public
index.php
Now assume that bootstrap.php
has some code included for setting up database connections or some other kind of boostrapping stuff.
现在假设其中bootstrap.php
包含一些用于设置数据库连接或其他类型的 boostrapping 的代码。
Assume you want to include a file in boostrap.php
's folder called init.php
. Now, to avoid scanning the entire include path with include 'init.php'
, you could use include './init.php'
.
假设您要在boostrap.php
名为 的文件夹中包含一个文件init.php
。现在,为了避免使用 扫描整个包含路径include 'init.php'
,您可以使用include './init.php'
.
There's a problem though. That ./
will be relative to the script that included bootstrap.php
, not bootstrap.php
. (Technically speaking, it will be relative to the working directory.)
不过有个问题。这./
将与包含的脚本有关bootstrap.php
,而不是bootstrap.php
. (从技术上讲,它将相对于工作目录。)
dirname(__FILE__)
allows you to get an absolute path (and thus avoid an include path search) without relying on the working directory being the directory in which bootstrap.php
resides.
dirname(__FILE__)
允许您获得绝对路径(从而避免包含路径搜索),而无需依赖工作目录作为所在目录bootstrap.php
。
(Note: since PHP 5.3, you can use __DIR__
in place of dirname(__FILE__)
.)
(注意:自 PHP 5.3 起,您可以使用__DIR__
代替dirname(__FILE__)
。)
Now, why not just use include 'init.php';
?
现在,为什么不直接使用 include 'init.php';
?
As odd as it is at first though, .
is not guaranteed to be in the include path. Sometimes to avoid useless stat()
's people remove it from the include path when they are rarely include files in the same directory (why search the current directory when you know includes are never going to be there?).
尽管一开始很奇怪,但.
不能保证在包含路径中。有时为了避免无用stat()
的人在很少包含同一目录中的文件时将其从包含路径中删除(为什么在知道包含永远不会存在的情况下搜索当前目录?)。
Note: About half of this answer is address in a rather old post: What's better of require(dirname(__FILE__).'/'.'myParent.php') than just require('myParent.php')?
注意:这个答案的大约一半是一个相当旧的帖子中的地址:require(dirname(__FILE__).'/'.'myParent.php') 比 require('myParent.php') 更好吗?
回答by M-J
I might have even a simpler explanation to this question compared to the accepted answer so I'm going to give it a go: Assume this is the structure of the files and directories of a project:
与接受的答案相比,我可能对这个问题有一个更简单的解释,所以我要试一试:假设这是一个项目的文件和目录的结构:
Project root directory:
file1.php
file3.php
dir1/
file2.php
(dir1
is a directory and file2.php
is inside it)
(dir1
是一个目录,file2.php
在里面)
And this is the content of each of the three files above:
这是上面三个文件中每个文件的内容:
//file1.php:
<?php include "dir1/file2.php"
//file2.php:
<?php include "../file3.php"
//file3.php:
<?php echo "Hello, Test!";
Now run file1.php
and try to guess what should happen. You might expect to see "Hello, Test!", however, it won't be shown! What you'll get instead will be an error indicating that the file you have requested(file3.php
) does not exist!
现在运行file1.php
并尝试猜测会发生什么。您可能希望看到“您好,测试!”,但是,它不会显示!相反,您将得到一个错误,表明您请求的文件( file3.php
) 不存在!
The reason is that, inside file1.php
when you include file2.php
, the content of it is getting copied and then pasted back directly into file1.php
which is inside the root directory, thus this part "../file3.php"
runs from the root directory and thus goes one directory up the root!(and obviously it won't find the file3.php
).
原因是,insidefile1.php
当你 include 时file2.php
,它的内容被复制,然后直接粘贴回file1.php
根目录中,因此这部分"../file3.php"
从根目录运行,因此在根目录下向上移动一个目录!(显然它不会找到file3.php
)。
Now, what should we do ?!
现在,我们该怎么办?!
Relative paths of course have the problem above, so we have to use absolute paths. However, absolute paths have also one problem. If you (for example) copy the root folder (containing your whole project) and paste it in anywhere else on your computer, the paths will be invalid from that point on! And that'll be a REAL MESS!
相对路径当然有上面的问题,所以我们必须使用绝对路径。然而,绝对路径也有一个问题。如果您(例如)复制根文件夹(包含您的整个项目)并将其粘贴到您计算机上的任何其他位置,则从那时起路径将无效!这将是一个真正的烂摊子!
So we kind of need paths that are both absolute and dynamic(Each file dynamically finds the absolute path of itself wherever we place it)!
所以我们需要既是绝对路径又是动态路径(每个文件在我们放置的地方动态地找到它自己的绝对路径)!
The way we do that is by getting help from PHP, and dirname()
is the function to go for, which gives the absolute path to the directory in which a file exists in. And each file name could also be easily accessed using the __FILE__
constant. So dirname(__FILE__)
would easily give you the absolute (while dynamic!) path to the file we're typing in the above code. Now move your whole project to a new place, or even a new system, and tada! it works!
我们这样做的方法是从 PHP 获取帮助,并且dirname()
是要使用的函数,它给出文件所在目录的绝对路径。每个文件名也可以使用__FILE__
常量轻松访问。所以dirname(__FILE__)
很容易为您提供我们在上述代码中键入的文件的绝对(同时是动态的!)路径。现在将你的整个项目移到一个新的地方,甚至是一个新的系统,多多!有用!
So now if we turn the project above to this:
所以现在如果我们把上面的项目变成这样:
//file1.php:
<?php include(dirname(__FILE__)."/dir1/file2.php");
//file2.php:
<?php include(dirname(__FILE__)."/../file3.php");
//file3.php:
<?php echo "Hello, Test!";
if you run it, you'll see the almighty Hello, Test!
! (hopefully, if you've not done anything else wrong).
如果你运行它,你会看到全能的Hello, Test!
!(希望,如果您没有做错任何其他事情)。
It's also worth mentioning that from PHP5, a nicer way(with regards to readability and preventing eye boilage!) has been provided by PHP as well which is the constant __DIR__
which does exactly the same thing as dirname(__FILE__)
!
另外值得一提的是PHP5,一个更好的方式(与问候的可读性和防止眼boilage!)已经由PHP提供的,以及它是恒定的__DIR__
,其不完全一样的东西dirname(__FILE__)
!
Hope that helps.
希望有帮助。
回答by Rocky Kumar
If you want code is running on multiple servers with different environments,then we have need to use dirname(FILE) in an include or include_once statement. reason is follows. 1. Do not give absolute path to include files on your server. 2. Dynamically calculate the full path like absolute path.
如果您希望代码在具有不同环境的多个服务器上运行,那么我们需要在 include 或 include_once 语句中使用 dirname( FILE)。原因如下。1. 不要给出绝对路径来包含服务器上的文件。2.像绝对路径一样动态计算完整路径。
Use a combination of dirname(FILE) and subsequent calls to itself until you reach to the home of your '/myfile.php'. Then attach this variable that contains the path to your included files.
结合使用 dirname( FILE) 和随后对自身的调用,直到您到达“/myfile.php”的主页。然后附加这个包含包含文件路径的变量。
回答by ironcove
I used this below if this is what you are thinking. It it worked well for me.
如果这是您的想法,我在下面使用了它。它对我来说效果很好。
<?php
include $_SERVER['DOCUMENT_ROOT']."/head_lib.php";
?>
What I was trying to do was pulla file called /head_lib.php from the root folder. It would not pull anything to build the webpage. The header, footer and other key features in sub directories would never show up. Until I did above it worked like a champ.
我试图做的是从根文件夹中提取名为 /head_lib.php 的文件。它不会拉任何东西来构建网页。子目录中的页眉、页脚和其他关键功能永远不会出现。直到我做到了上面它的工作就像一个冠军。