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?
我为什么要这么做?为什么我要麻烦地获取dirname,然后用目录分隔符和新文件名连接它呢?
Is the code above not equivalent to this:
上述守则是否与以下条文不等同:
<?php
include( 'my_file.php' );
?>
??
? ?
The PHP doc says,
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. 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()不同,后者会产生致命错误。
3 个解决方案
#1
56
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.
现在,假设引导。php包含了一些用于设置数据库连接的代码或其他启动程序。
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.)
不过有一个问题。这是相对于包含引导程序的脚本的。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__)允许您获得绝对路径(从而避免包含路径搜索),而不依赖于工作目录作为引导程序的目录。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?).
虽然一开始很奇怪,不能保证在include路径中。有时,为了避免无用的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”)比“myParent.php”更好吗?
#2
0
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(文件)和后续调用的组合,直到到达'/myfile.php'的home。然后将包含路径的变量附加到所包含的文件中。
#3
0
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的pulla文件。来自根文件夹的php。它不会拉出任何东西来构建网页。子目录中的页眉、页脚和其他关键特性将永远不会出现。直到我超越它,它就像一个冠军。
#1
56
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.
现在,假设引导。php包含了一些用于设置数据库连接的代码或其他启动程序。
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.)
不过有一个问题。这是相对于包含引导程序的脚本的。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__)允许您获得绝对路径(从而避免包含路径搜索),而不依赖于工作目录作为引导程序的目录。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?).
虽然一开始很奇怪,不能保证在include路径中。有时,为了避免无用的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”)比“myParent.php”更好吗?
#2
0
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(文件)和后续调用的组合,直到到达'/myfile.php'的home。然后将包含路径的变量附加到所包含的文件中。
#3
0
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的pulla文件。来自根文件夹的php。它不会拉出任何东西来构建网页。子目录中的页眉、页脚和其他关键特性将永远不会出现。直到我超越它,它就像一个冠军。