Reputation: 1438
So I have domain.be/index.php
Then I have require Controllers/indexController.php
;
In that file I have require ../Model/opalus.php
;
So I builded the first controller, for my framework which worked, yèeey.
But If I use my controller in the index.php I get
PHP Fatal error: require(): Failed opening required '../Model/opalus.php' (include_path='.:/opt/cpanel/ea-php56/root/usr/share/pear') in /home/webkeuke/public_html/Controllers/indexController.php on line 2
I have tried using _DIR__
no ../
or ./
etc, can somebody please fix this?
Upvotes: 0
Views: 54
Reputation: 136
Sometimes multiply file inclusions by relative Paths (../
, ./
) in PHP on complex projects can make a lot of problems especially in error tracing.
To prevent your project and yourself to stuck in this problem I recommend to do all file inclusions with fullpaths (CHANGE include("../inc/extra.php"
- TO include("/var/www/myproject/inc/extra.php"
).
But this method can also be make a lot of work if your filesystem paths changes (f.e. when you change your Server environment or the directory then you have to change all files with an include in it).
So the best solution is to define the basic filesystem path in a global variable at a point where your scripting starts. Then use the defined basic filesystem path variable on every include();
Example for your case (index.php):
<?php
define ("FSPATH","/home/webkeuke/public_html/");
[your code]
// include /home/webkeuke/public_html/Controllers/indexController.php
include (FSPATH."Controllers/indexController.php");
[your code]
?>
Example for your case (Controllers/indexController.php):
<?php
[your code]
// include /home/webkeuke/public_html/Model/opalus.php
include (FSPATH."Model/opalus.php");
[your code]
?>
Ths solution should prevent you to get stucked at a point with wrong include Paths.
Upvotes: 1