Search code examples
phpapache.htaccessxampp

Require(): Failed opening required (include_path='.;C:\xampp\php\PEAR')


I have created a website using mvc framework, but I cant include php files in files that are from another folder. For example, I have model, view, controller and core folders, if I do the following require 'Core/User.php'; in a view file in view folder, then I get the following error:

Warning: require(Core/User.php): failed to open stream: No such file or directory 
in C:\xampp\htdocs\k2\View\customer-management.php on line 2

Fatal error: require(): Failed opening required 'Core/User.php' 
(include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\k2\View\customer-management.php 
on line 2

I've tried using ../Core/User.php, but the application becomes too messy and i need a cleaner way of doing this, perhaps working with php.ini or .htaccess to allow me to just include('Core/User.php'); from another folder. Any help would be appreciated.


Solution

  • There are two ways that I would do this. Both are ways of adding 'C:\xampp\htdocs\k2' to your include path

    1. Use .htaccess

    Add this line to you .htaccess file in the web root. You can add other directories as desired

    php_value include_path '.;C:\xampp\php\PEAR;C:\xampp\htdocs\k2'
    

    Makse sure Apache is configured to allow htacess to work: Number 4

    1. If you have some "config" file that is always included, you can add this line to it to set the include path

    set_include_path(get_include_path() . PATH_SEPARATOR . 'C:\xampp\htdocs\k2');

    This makes your code more portable than using relative or absolute path, since there is only one place to make changes when file structures change.