r/PHPhelp • u/ardicli2000 • Jun 30 '25
include relative path problem with __DIR__
include __DIR__ . "/../../../db_config.php"; //DOESNOT WORK
$dir = __DIR__;
include "$dir/../../../db_config.php"; //WORKSThis 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
2
u/flyingron Jun 30 '25
Do you want to give us a clue as to what error you get in the first case that "doesn't work?"
1
u/ardicli2000 Jun 30 '25
Errors i get,
If the file is in document root, I get no such file found error.
If it is outside of document root I get permission denied error
2
u/colshrapnel Jun 30 '25
I do understand that your are confused, and your lack of knowledge is coupled with numerous mistakes and typos, making it even more confused.
But you must understand too, that you need to learn how to explain your problems to other people. You keep talking about things like "server root", "document root", "doesn't work" and such. The problem is, other people have not a slightest idea what these paths and errors are.
You have been told already that instead of pointless "doesn't work" you are supposed to provide full error message. Yet you keep with these pointless "doesn't work".
Now brace yourself and ask a proper question.
1
u/colshrapnel Jun 30 '25
Obviously, both paths are equal. Hence the problem is some silly mistake, like wrong nesting count.
How to resolve it: read the error message, which is extremely helpful with filesystem operations. It displays the attempted path along with actual path to the current file, so you can compare them and adjust the wrong path.
How to never encounter it: always use absolute paths from the site root. Either by using DOCUMENT_ROOT or by using a single entry point and defining a constant that contains the site root path. See https://phpdelusions.net/articles/paths
1
u/eurosat7 Jun 30 '25 edited Jun 30 '25
There is a standard for that.
A common structure for big apps compatible to symfony is like this:
That makes it easy to simplify the files accessible inside the public folder:
And yes, your DOCUMENT_ROOT should point to the public folder. Now you can do everthing inside the bootstrap relative from the project root folder:
PS: This example is not 100% symfony, I know. That's not the point here.
I hope you have it easier now.
something I did for redditors like you: https://github.com/eurosat7/csvimporter/blob/main/bootstrap.php You might want to take a look.
And there is a brilliant (really!) article from the symfony bubble to walk you through the decision making which will help you understand why some things are the way they are in most of modern code:
https://symfony.com/doc/current/create_framework/index.html