PHP include_path issue
include(‘Helper.php’);
include(‘Enchance.php’);
// Third party functions
include(‘misc.php’);
are all failing because php search in the wrong path and can’t find the files. Sure, adding absolute path works but I can’t do this on all files. Any suggestions ?
Just to be clear, it's not about my code, I've set up a web server on a linode and then installed wordpress, many plugins and themes give errors because of this. How to set include_path in php.ini (or maybe other settings) to not give errors when working with relative paths ?
Thank you.
2 Replies
(in your front-end files)
// index.php
define('ROOT_PATH', dirname(__FILE__));
// [...]
include ROOT_PATH . '/includes/mydirectory/myfile.php';
(deeper inside your application, front-end file in a subdirectory)
// subdirectory/index.php
define('ROOT_PATH', dirname(dirname(__FILE__)));
// [...]
include ROOT_PATH . '/includes/mydirectory/myfile.php';
You can also use that deeper in your code to maintain clear and understandable include paths, as well. Since all includes would be from the perspective of your forward-facing files, it'd mimic the behavior of using the include_path while still allowing yourself adaptability. To maintain your root path, just adjust your constant delcaration (and instead of using .. in the root path, use dirname