Search code examples
phppathinclude-path

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

But the file location is actually right? It even gives me auto path fill suggestions (visual code plugin) to that path!

Why does this happen is this something to do with the / or the second warning line?

 

So I'm trying to include the settings.php file from inside the User.class.php file with the following code.

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

../ -> goes back into the root folder

settings/ -> enters settings folder

settings.php -> should locate the needed .php file

enter image description here

enter image description here


Solution

  • Replace

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

    with

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

    __DIR__ will resolve in the absolute path of the file where this constant is used. You can navigate your folders from that.