2
I have a problem when my project has a structure with a large number of directories. When I do include in the file the url is relative to the page that was opened. And then in some files the includes are not recognized.
For example I open a page with depth 3 of directory, dou include in a file with depth 2, and inside that file the includes that it has do not reconhcer the files because the url is in relation to the first page that I opened.
not to mention that many files need to do this ../../../arquivo.php
to reconhcer files from other directories.
STRUCTURE EXAMPLE
.
├── controller
│ └── entityController.class.php
│
│
├── model
│ └── entity.class.php
│
|
└── ws
├── dir
│ └── page1.php
└── dir2
└── page2.php
SITUATION
first page1 opens on it has a include a entityController.class.php at two levels above for access
include '../../controller/entityController.class.php';
2nd entityController has a include to Entity that needs to return to a level above to be able to access
include '../model/entity.class.php';
Then the problem arises, this include does not work because it occurs in relation to the level of the page1.php file that was the first to be called, so it will search the folder model
inside the directory ws
, I mean, to make it work, I’d have to do it this way
include '../../model/entity.class.php';
But it would solve in this case, and it would already be a problem because the controller is called by other files of different levels.
in the file that makes the call to include, just need to know the basis itself. To do this, just do something like $base = dirname(FILE) . DIRECTORY_SEPARATOR;
– Daniel Omine
This space for comment is very limited.. where dirname(FILE) is dirname(_ _ FILE _ _) remove spaces within the parentheses...
– Daniel Omine
another option is realpath('.').. but I do not recommend using it as it can return a different path when executed via command line/shell.
– Daniel Omine
@Danielomine Your comment is already the solution to his problem. Pass to an answer that is more readable and he can mark as accepted.
– André Ribeiro
I know André Ribeiro, but I don’t have the balls to write a megalomaniac reply.. And if I post something simple, practical and objective, a "***" will appear to be negative.. rsrsr I hope you understand.. Whoever has the will, get to work!
– Daniel Omine
I’ve tried using dirname( _ FILE _ ), and up to _ DIR _ _ , but it didn’t work tbm, as this shows the full url of the first file that was opened, and this happens
...../ws/dir/controller/entityController.class.php
.– Skywalker
used
$_SERVER['DOCUMENT_ROOT']
and it worked, I’ll do some tests.– Skywalker
@Juarez consider posting a reply explaining how you used DOCUMENT_ROOT, so other visitors with similar problem can take advantage of your solution (which, in my view, is the most correct, as it depends on where you are giving the include - although the title of your question suggests relative path)
– Bacco
I would post the answer but @Kaduamaral had already answered clearly how the
__DIR__
so I didn’t think it was fair to post.– Skywalker