0

There's a little library I'm trying to plug in to my project. This lib has some includes and requires across classes, so I'm trying to set an include path for all that to work.

When trying to set an include path off a sibling branch, I run into a hiccup.

For a reference point, require('/../my/test.php') works fine.

So does

set_include_path('/../');
require_once('my/test.php');

But once I try

set_include_path('/../my/');
require_once('test.php');

I get:

Warning: require_once(one.php): failed to open stream: No such file or directory in ...

What am I missing?

mOrloff
  • 2,547
  • 4
  • 29
  • 48
  • 1
    Don't use relative paths like that, it's very easy to mess everything up. Refer to the CURRENT FILE using `dirname(__FILE__)` and build relative paths from that, in every single file. Just my preference. Example: `require_once(dirname(__FILE__).'/../my/test.php');` – Eggplant Feb 25 '13 at 17:04

1 Answers1

5

Starting your paths with / means look in the root directory, so /../ is technically one directory above the root directory.

To set the include path to the parent directory of the current location, you just need ../. To make the code more portable I would suggest combining it with dirname(__FILE__) to get the absolute path of the current directory.

IE:

set_include_path(dirname(__FILE__) . '/../my/');

Note the preceding / is required in that example as dirname() does not return a trailing slash

Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
  • thanks, I'll mark this as the answer as soon as I'm allowed to. – mOrloff Feb 25 '13 at 17:10
  • Using `dirname(__FILE__)` to pass it to `set_include_path()` is a complete contraddiction in my opinion. What he's asking is a way to not be bothered with relative paths in DIFFERENT files included (file A includes file B which includes file C, etc...). See my comment above. – Eggplant Feb 25 '13 at 17:11
  • 1
    I know, I certainly wouldn't advocate `set_include_path()` abuse on a project. OP is better off just requiring from the direct paths. However the question is "Why isn't my `set_include_path()` working" – Dunhamzzz Feb 25 '13 at 17:14
  • 1
    Am I understanding correctly that the preferred suggestion is that I go through the entire library and update **all** their `require()` statements?, or ????? – mOrloff Feb 25 '13 at 17:24
  • @mOrloff There's multiple ways of doing it, I'd leave things be if its going to be a lot of work to change and it's all working fine. – Dunhamzzz Feb 25 '13 at 17:30