1

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?

beatjunky99
  • 149
  • 11
  • PHPs `include_path` is only used for unanchored paths (no leading `/` or `.` or `..` relations). If you're unable to use relative paths, use `$_SERVER["DOCUMENT_ROOT"]`-bound ones. – mario Jun 05 '14 at 01:36
  • 2
    @mario no no no! Why introduce an external dependency like document root? It excludes you from running scripts under any environment other than a web server. – Phil Jun 05 '14 at 01:43

1 Answers1

0

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.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • Have tried this, but there is no change. still the same warning, unless the path is now the other way round: 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 67 – beatjunky99 Jun 05 '14 at 01:28
  • @bj99 Why are you still including `../res` when calling `include`? My answer specifically shows you what to use – Phil Jun 05 '14 at 01:29
  • look at the changes I made to my initial post. I cant change the var $web_proc. :-/ And also the files included by check_sprache.php are included by using the same variable $web_proc in the include statement. – beatjunky99 Jun 05 '14 at 01:31
  • @bj99 Yeah, would have been nice to know that initially. Relative paths **with** directory traversal in a config file sounds like a ridiculous idea. Also, what's the point in having a config file you can't change? – Phil Jun 05 '14 at 01:39
  • the config file is given from site admin, and changing would break the include behaviour of the rest of the the page :-( So it's not possible to use relative paths with set_include_path? – beatjunky99 Jun 05 '14 at 01:42
  • @bj99 So where does the *"site admin"* expect that path to be relative from? It's just stupid. Anyway, why can't you ignore the config file and write code that works? – Phil Jun 05 '14 at 01:44
  • two folders above the folder i'm working in. But have now contacted the admin, since i think instead of defining `$web_proc = '../res/proc';` it will be the same to do: `$paths = array(dirname(__DIR__), get_include_path()); set_include_path(implode(PATH_SEPARATOR, $paths));` in the config file and leave away the dots (`$web_proc = 'res/proc';`)! – beatjunky99 Jun 05 '14 at 01:51