aasd
aasd

Reputation: 129

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.

Upvotes: 6

Views: 69558

Answers (2)

Carl-Binneman
Carl-Binneman

Reputation: 11

New to PHP, sorry if this is an invalid answer;

The code that produced the error for me :

require_once '..app/controllers/' . $this->currentController . '.php';

    

The issue was that I did not have a / after the .. Correcting to the following code solved the error :

require_once '../app/controllers/' . $this->currentController . '.php';  

  

Upvotes: 1

Dan
Dan

Reputation: 11084

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.

Upvotes: 6

Related Questions