php 在 windows 上包含路径
php include path on windows
我在 Windows 上与 PHP 一起工作了几天。我被指示的整个时间包括以下方式:
在 index.php 文件中:
require ('include/init.inc');
在目录"include"中我声明:
在 init.inc 文件中:
require ("workbench.inc");
但是突然不能用了。一次我还必须在 "init.inc" 中指定目录。
为什么一直在工作,突然就没有了?
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
因此 current working directory (CWD) 在包含其他文件时很重要。可以通过getcwd()
检索。
如果你的目录结构是这样的
rootdir/
include/
init.rc
workbench.rc
index.php
那么 require('include/init.rc')
只有在 rootdir/
是 CWD 或者是搜索路径的一部分时才有效。同样,require('init.inc')
假定 CWD 为 rootdir/include/
。由于当前工作目录可以更改,因此在 PHP 中使用更强大的
是惯用的
// in index.php
require(__DIR__ . '/include/init.rc');
// in workbench.rc
require(__DIR__ . '/init.rc');
那么 require
将独立于 CWD 工作。这是有效的,因为 magic constant __DIR__
被包含常量的文件的绝对路径替换,没有尾随目录分隔符,例如
- 在
index.php
中,__DIR__
是D:\path\to\rootdir
和
- 在
include/init.rc
中,__DIR__
是D:\path\to\rootdir\include
。
我在 Windows 上与 PHP 一起工作了几天。我被指示的整个时间包括以下方式:
在 index.php 文件中:
require ('include/init.inc');
在目录"include"中我声明:
在 init.inc 文件中:
require ("workbench.inc");
但是突然不能用了。一次我还必须在 "init.inc" 中指定目录。 为什么一直在工作,突然就没有了?
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
因此 current working directory (CWD) 在包含其他文件时很重要。可以通过getcwd()
检索。
如果你的目录结构是这样的
rootdir/
include/
init.rc
workbench.rc
index.php
那么 require('include/init.rc')
只有在 rootdir/
是 CWD 或者是搜索路径的一部分时才有效。同样,require('init.inc')
假定 CWD 为 rootdir/include/
。由于当前工作目录可以更改,因此在 PHP 中使用更强大的
// in index.php
require(__DIR__ . '/include/init.rc');
// in workbench.rc
require(__DIR__ . '/init.rc');
那么 require
将独立于 CWD 工作。这是有效的,因为 magic constant __DIR__
被包含常量的文件的绝对路径替换,没有尾随目录分隔符,例如
- 在
index.php
中,__DIR__
是D:\path\to\rootdir
和 - 在
include/init.rc
中,__DIR__
是D:\path\to\rootdir\include
。