I need to include a file in php so I do
$web_proc = '../res/proc'; //variable read from config file I can't change
//in my file
include_once($web_proc . '/check_sprache.php');
and PHP outputs:
Warning: include_once(../res/proc/check_sprache.php): failed to open stream: No such file or directory in /Users/user/Sites/site/res/pdf/rechnung.php on line 62
Warning: include_once(): Failed opening '../res/proc/check_sprache.php' for inclusion (include_path='.:') in /Users/user/Sites/site/res/pdf/rechnung.php on line 62
So I change the include path:
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__DIR__) . DIRECTORY_SEPARATOR);
and try it again, but PHP outputs:
Warning: include_once(../res/proc/check_sprache.php): failed to open stream: No such file or directory in /Users/user/Sites/site/res/pdf/rechnung.php on line 62
Warning: include_once(): Failed opening '../res/proc/check_sprache.php' for inclusion (include_path='.::/Users/user/Sites/site/res/') in /Users/user/Sites/site/res/pdf/rechnung.php on line 62
But if if I do
include_once(dirname(__DIR__). DIRECTORY_SEPARATOR . $pfadweb_proc . '/check_sprache.php');
it works. But this is no solution, since the included file includes more files with a relative path, so they are alse not found.
So either I misunderstand the PHP include path, or it's just trolling me.
Can anybody help?
The include path looks odd in the second example; You have two PATH_SEPARATOR
characters and a trailing DIRECTORY_SEPARATOR
though I doubt this is the problem.
Try this one out instead
set_include_path(implode(PATH_SEPARATOR, [ // use array() if PHP < 5.4
dirname(__DIR__), // this should be /Users/user/Sites/site/res
get_include_path()
]));
include_once 'proc/check_sprache.php';
This will add the parent directory (/Users/user/Sites/site/res
) of the CWD (/Users/user/Sites/site/res/pdf
) to your include path.
Any include
statements in any other files can use a relative path from /Users/user/Sites/site/res
.