无法获取include(目录名(__FILE__)"/dir/file.php”);工作


cannot get include(dirname(__FILE__) . "/dir/file.php"); to work

好的,所以我的一个include有问题。我的config.php包含一个名为user.php的文件,该文件位于classes目录中。如果我从不同的目录调用config.php文件,相对路径就会断开。

我试过使用

include(dirname(__FILE__) . "/classes/user.php");

include __DIR__ . '../classes/user.php'; 

而不是

include('../classes/user.php');

在我的config.php文件中,它仍然会给我一个cannot文件或目录错误。它一直在includes/classes/user.php中查找user.php,而不是在目录中查找。我已经尝试过使用../和不使用斜杠的目录名方式。似乎什么都不管用。

为了澄清,我的结构如下:

-admin
-classes
--user.php
-css
-drop
--index.php
-img
-includes
--config.php
-js
index.php
etc.

config.php需要classes/user.php。当我试图将config.php包含在drop/index.php文件中时,我遇到了一个问题。它找不到类/user.php。

您是否尝试在两个点(..)之前添加反斜杠?像这个

include(dirname(__FILE__) . "/../classes/user.php");

我只是想尽力帮助你。

也许,试试这样的东西:

include($_SERVER['SERVER_NAME'] . "/classes/user.php");

更新,如果../和以上也不起作用,请尝试以下操作:

 include(dirname(dirname(__FILE__)) . "/classes/user.php");