Zanic L3
Zanic L3

Reputation: 1088

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

Upvotes: 2

Views: 5435

Answers (3)

Gabriel
Gabriel

Reputation: 970

Depending on where you call your class. Better yet is to use configuration file to fulfil the require/include path like every PHP framework does.

Upvotes: 0

Federkun
Federkun

Reputation: 36989

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.

Upvotes: 5

Karlo Kokkak
Karlo Kokkak

Reputation: 3714

Use an absolute path to the file. You can use $_SERVER['DOCUMENT_ROOT'] or create a const variable BASEPATH that you can easily set root directory value to.

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

OR

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

Upvotes: 1

Related Questions