警告:include_once(../settings/settings.php): 无法打开流 - 但路径实际上是正确的?

Warning: include_once(../settings/settings.php): failed to open stream - but the path is actually right?

Warning: include_once(../settings/settings.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/Project/classes/user.class.php on line 2

Warning: include_once(): Failed opening '../settings/settings.php' for inclusion (include_path='.:/Applications/MAMP/bin/php/php7.2.1/lib/php') in /Applications/MAMP/htdocs/Snapshot/classes/user.class.php on line 2

但是文件位置其实是对的?它甚至为我提供了该路径的自动路径填充建议(可视代码插件)!

为什么会出现这种情况,这与 / 或第二条警告线有关吗?

所以我试图从 User.class.php 文件中包含 settings.php 文件以下代码。

include_once("../settings/settings.php");

../ -> 返回到根文件夹

settings/ -> 进入设置文件夹

settings.php -> 应该找到所需的 .php 文件

使用文件的绝对路径。您可以使用 $_SERVER['DOCUMENT_ROOT'] 或创建一个 const 变量 BASEPATH ,您可以轻松地将根目录值设置为。

include_once($_SERVER['DOCUMENT_ROOT']."/settings/settings.php");

define('BASEPATH', "/Applications/MAMP/htdocs");
include_once(BASEPATH."/settings/settings.php");

替换

include_once("../settings/settings.php");

include_once(__DIR__ . "/../settings/settings.php");

__DIR__ 将在使用此常量的文件的绝对路径中解析。您可以从中导航您的文件夹。

取决于您在哪里打电话 class。更好的是使用配置文件来完成 require/include 路径,就像每个 PHP 框架一样。