php.ini中的include_path指令影响在写入文件的非限定名称时首先查看当前目录


include_path directive in php.ini affects looking first at the current directory when writting an unqualified name of a file?

此指令允许设置文件函数查找以非限定名称(例如hello.txt)写入的文件的路径。在这种情况下,我的问题是:这些函数总是先在当前目录中查找,然后在include_path目录中写入的路径中查找吗?还是因为第一个路径是".",即当前目录,所以这些函数在当前目录中首先出现?

执行文件操作时,PHP会"查看"当前工作目录。您可以通过调用getcwd来获取该目录。一开始我发现这很令人困惑,因为在使用relativ路径(比如"不合格的文件名")时,它可能会导致"意外"行为,这就是为什么它养成了总是使用__DIR__的习惯(请参阅http://php.net/manual/en/language.constants.predefined.php)以定义起始位置。示例:

//script location: /foo/bar/example.php
$filename = "hello.txt";
$filepath = __DIR__."/".$filename;
// $filepath is /foo/bar/hello.txt

如果您尝试将includerequire另一个文件添加到php脚本中,但与基于文件的操作无关,则会考虑include_path