File include problems, pointing directory against the page that was opened first

Asked

Viewed 2,982 times

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;

  • This space for comment is very limited.. where dirname(FILE) is dirname(_ _ FILE _ _) remove spaces within the parentheses...

  • another option is realpath('.').. but I do not recommend using it as it can return a different path when executed via command line/shell.

  • @Danielomine Your comment is already the solution to his problem. Pass to an answer that is more readable and he can mark as accepted.

  • 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!

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

  • 1

    used $_SERVER['DOCUMENT_ROOT'] and it worked, I’ll do some tests.

  • @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)

  • I would post the answer but @Kaduamaral had already answered clearly how the __DIR__ so I didn’t think it was fair to post.

Show 4 more comments

1 answer

1


Every time you put one include or require use the absolute path instead of relative path, this is very easy using the "magic" constants. Try it like this:

pager1

include __DIR__.'/../../controller/entityController.class.php'

entityController

include __DIR__.'/../model/entity.class.php';

Note that the constant __DIR__ does not have the last bar, making it necessary to place it.

The constant __DIR__ returns the directory name of the current file (the file that is using it) the same as dirname( __FILE__ ).

  • It worked, thank you, I was using incorrectly, I was not using /../ after concatenation, did not know it could work this way, another method that I thought worked was to use $_SERVER['DOCUMENT_ROOT'].'/model/entity.class.php then you don’t have to pass back parameters /../. It’s such a simple thing I already knew, but I couldn’t find a place that would explain how to use it. Now I won’t have any doubts about how it works.

  • So @Juarez, I never used this $_SERVER['DOCUMENT_ROOT'], I just learned something from your question, rsrs. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.