简体   繁体   中英

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

Okay, so I'm having an issue with one of my includes. My config.php includes a file called user.php which is in classes directory. If I call the config.php file from a different directory, relative paths get broken.

I tried using

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

and

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

instead of

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

in my config.php file and it still gives me a cannot file or directory error. It keeps looking for user.php in includes/classes/user.php instead of going up a directory. I've tried the dirname way both with ../ and without the slash. Nothing seems to work.

To clarify, my structure looks like this:

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

config.php requires classes/user.php. I get an issue when I try to include config.php in the drop/index.php file. It can't find the classes/user.php.

Do you have tried to add a backslash before two dots (..)? Like this

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

Just want to try to help you.

Perhaps, try something like this:

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

UPDATE, if ../ and the above doesnt work either, try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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