skip to Main Content

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

relative directory of files

It works only if i write the whole path starting from my Local Disk=>
‘D:SetupxampphtdocsBOB2.1Teacher-Assistant-SWmodelsdb_controller.php’

2

Answers


  1. 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

    <?php
    
    require_once './folder/file2.php'; // works fine
    

    File 2

    <?php
    
    require_once './file3.php'; // Throw error
    

    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

    <?php
    
    require_once __DIR__ . '/file3.php';
    

    enter image description here

    Login or Signup to reply.
  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. =)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search