I face strange problem in writing paths using function "requrire_once
".
There is file open file that open file that open file, Only the last got the error of "Failed to open stream: No such file or directory", which all of them have been written by the same way of writing the relative path
first file => require_once '../models/user/student.php';
second file => require_once 'user.php';
third file => require_once '../db_controller.php';
The error =>
Warning: require_once(../db_controller.php): Failed to open stream: No such file or directory in D:SetupxampphtdocsBOB2.1Teacher-Assistant-SWmodelsuseruser.php on line 3
It works only if i write the whole path starting from my Local Disk=>
‘D:SetupxampphtdocsBOB2.1Teacher-Assistant-SWmodelsdb_controller.php’
2
Answers
The reason you are getting this error is because the path is relative to the original file executing the code. Take this structure for example.
File 1
File 2
The solution is to obtain the current directory then add your relative path. So to fix our example we would just need to make this simple change.
File 2
Accroding to the error message, you were trying to call ‘../db_controller.php’ in
‘./models/user/user.php’. Which means the path you were calling is ‘./models/user/../db_controller.php’ => ‘./models/db_controller.php’.
So, the error is correct, the path is not exists. I usually use root path to avoid this. =)