Php – Require_once failed to open stream: No such file or directory

PHPrequire-once

I have this error:

Warning: require_once(../questionTypes/Question.php): failed to open stream: No such file or directory in /home/u949913003/public_html/includes/essential.php on line 25

Fatal error: require_once(): Failed opening required '../questionTypes/Question.php' (include_path='.:/opt/php-5.6/pear') in /home/u949913003/public_html/includes/essential.php on line 25

I require essential.php from New_test.php like this:

require_once('../essential.php');

Then in essential.php, I require Question.php like this:

require_once($config['systemQuestionTypesClassDir'].'Question.php');

$config is defined in config.php:

$config['controller']['a'] = 'Admin';
$config['controller']['e'] = 'Teacher';
$config['controller']['t'] = 'Teacher';
$config['controller']['s'] = 'Student';
$config['controller']['at'] = 'Teacher';
$config['controller']['er'] = 'Teacher';
// System directories
$config['systemControllersDir'] = '../controllers/';
$config['systemQuestionTypesClassDir'] = '../questionTypes/';
$config['systemViewsDir'] = '../views/';
$config['systemLibsDir'] = 'libs/';
$config['systemLangsDir'] = 'langs/';
$config['systemQuestionTypesLibDir'] = $config['systemLibsDir'].question;
$config['systemLangsXml'] = '../resources/languages/';
$config['systemExtraDir'] = = 'extra/';
$config['systemFpdfDir'] = 'fpdf/';
$config['systemPhpGraphLibDir'] = 'phpgraphlib—master/';
$config['systemFileManagerDir'] = 'filemanager/';

My folders structure is this:

enter image description here

Obviously in questionTypes there is Question.php.

I don't know ho to solve this.

Best Answer

Calling "include" from another "include" could cause problems when they are relative paths. If you use the absolute path, it shouldn't be a problem.

You could use __DIR__ to get the directory that contains the file where this is written.

I think this should works:

File: New_test.php

require_once(__DIR__ .'/../essential.php');

File: essential.php

require_once(__DIR__ .'/../questionTypes/Question.php');