简体   繁体   English

PHP include():用例来自原始脚本的PWD

[英]PHP include(): Use case for include from original script's PWD

When using PHP's include() and require() functions to include a file which in turn includes another file, the PWD (relative reference) for the second file's include() is the directory for the original script location (the first script in the stack as called by Apache), not for the current file. 当使用PHP的include()require()函数来包含一个文件,而该文件又包含另一个文件时,第二个文件的include()的PWD(相对引用include()是原始脚本位置的目录(栈中的第一个脚本)由Apache调用,不适用于当前文件。 What is the design decision behind that, and what is the use-case? 背后的设计决策是什么,用例是什么?

For instance, suppose a generic database-connection class defined in ~/public_html/classes/database.php which stores its configuration data (usernames, passwords) out of the web root in ~/config.php . 例如,假设在~/public_html/classes/database.php定义了一个通用数据库连接类,它将配置数据(用户名,密码)存储在~/config.php中的Web根目录之外。 The author of the database configuration class would logically call the config file with the relative filename ../../config.php . 数据库配置类的作者将在逻辑上使用相对文件名../../config.php调用配置文件。 However, this does not work as expected because the PWD is not of the database.php file but rather of the file which included it, which could be ~/public_html/index.php , ~/public_html/someDir/somePage.php or elsewhere. 但是,这不能按预期工作,因为PWD不是database.php文件,而是包含它的文件,可能是~/public_html/index.php~/public_html/someDir/somePage.php或其他地方。

I know to work around this by getting the directory of the current file with dirname(__FILE__) . 我知道通过使用dirname(__FILE__)获取当前文件的目录来解决这个问题。 However, I cannot think of a single use case where I would want require() or include() to be relative to the original script location. 但是,我想不出一个用例,我希望require()include()相对于原始脚本位置。 What is the use case for it being as it is? 它的用例是什么?

The function include() and require() handle relative path the same as any other file access function like fopen() , unlink() and mkdir() . 函数include()require()处理相对路径与fopen()unlink()mkdir()等任何其他文件访问函数相同。

Path are always relative to PWD regardless of the location of the script. 无论脚本的位置如何,路径始终与PWD相关。

Take the following example into consideration: 考虑以下示例:

/home/foo/index.php does /home/foo/index.php

require_once('lib/maketest.php');

/home/foo/lib/maketest.php does /home/foo/lib/maketest.php

mkdir('test');

This will create the directory /home/foo/test and not /home/foo/lib/test . 这将创建目录/ home / foo / test不是 / home / foo / lib / test


I can image that this has chosen because a bash script that does mkdir test will also create the directory $PWD/test regardless of where the script is located. 我可以想象这已经选择了,因为执行mkdir test的bash脚本也会创建目录$PWD/test无论脚本位于何处。

Having include() following the same logic as other file access functions makes sense. include()遵循与其他文件访问函数相同的逻辑是有意义的。 Consider the following code: 请考虑以下代码:

if (file_exists('config.php')) include('config.php');

If paths in include() would be relative to __DIR__ and paths in file_exists() to PWD the above code wouldn't work as expected. 如果include()路径相对于__DIR__ ,而file_exists()路径相对于PWD,则上述代码将无法按预期工作。

PWD is short for Print Working Directory. PWD是打印工作目录的缩写。 Current working directory is abbreviated as CWD. 当前工作目录缩写为CWD。
The CWD might change during the code execution and PHP doesn't necessarily have to include code from a local file path. CWD可能在代码执行期间发生变化,PHP不一定要包含本地文件路径中的代码。

As you already knew, examining __FILE__ together with dirname() is the correct solution to your problem. 如您所知,将__FILE__dirname()一起检查是您问题的正确解决方案。 You should also be defining a constant with the root path of your application and use that throughout the code instead of relying on that the working directory stays the same. 您还应该使用应用程序的根路径定义常量,并在整个代码中使用该常量,而不是依赖于工作目录保持不变。
The CWD will not even be the path of your script starting point in all situations. 在所有情况下,CWD甚至都不是脚本起点的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM