r/PHPhelp • • Jun 30 '25

include relative path problem with __DIR__

include __DIR__ . "/../../../db_config.php";  //DOESNOT WORK

$dir = __DIR__;
include "$dir/../../../db_config.php"; //WORKS

This is the case for php 8.2 and above. On the other hand both work perfectly fine with php81.

What may be the case here? Any ideas?

Edit:

I have debugged a little bit more:

CASE 1:

  • db_config.php is under htdocs/kayit folder.
  • include "./../../db_config.php"; WORKS
  • include DIR . "./../../db_config.php"; DOESN'T WORK
  • include DIR . "../../db_config.php"; WORKS

CASE 2: db_config.php is at the same directory with config.php, namely: htdocs/kayit/abant2025

  • include DIR . "./../db_config.php"; DOESN'T WORK
  • include "./../db_config.php"; WORKS
  • include DIR . "./db_config.php"; DOESN'T WORK
  • include DIR . "/db_config.php"; WORKS

CASE 3: db_config is under test directory (same directory with htdocs and outside of server directory)

  • include DIR . "......\db_config.php"; DOESN'T WORK. Says no such file or directory found.
  • include DIR . "./../../../db_config.php"; DOESN'T WORK. triggers is not within the allowed path(s) error
  • include "./../../../db_config.php"; DOESN'T WORK. no file or directory

no way to include it when outside of DocumentRoot.

3 Upvotes

19 comments sorted by

View all comments

Show parent comment

2

u/colshrapnel Jun 30 '25

Wait what, it was you? So you didn't learn your own lesson?

1

u/ardicli2000 Jun 30 '25

Just bcs folder name is xampp it does not mean it is xampp.

If one drive is the issue, then why does it work without an issue with php81

1

u/colshrapnel Jun 30 '25

Frankly, nobody has any idea. It can be anything. Any kind of silly mistake. Anyway, it shouldn't be your concern how it worked before, you must focus on your current problems. Move your php files from one drive, remove completely anything related to xammp, install mysql, download PHP and run built-in server. Everything would work

1

u/ardicli2000 Jun 30 '25

I did run tests outside of one drive too. Still got the same error. I need to sort everything before moving to new setup completely

1

u/colshrapnel Jun 30 '25

Which error? Post it here