2

Possible Duplicate:
PHP: Why such weird behaviour when I include a file from another directory which also includes a file?

I have a problem including a file that has an included file.

  • core.inc.php PATH: www/test/includes/core.inc.php

  • included file in core.inc.php : include_once ('../../connectdb.php');

  • connectdb.php PATH: www/connectdb.php

  • index.php PATH: www/test/index.php

  • included file in index.php : include_once ('included/core.inc.php');

When I run index.php the following warnings are popping up:

(!) Warning: include_once(../../connectdb.php): failed to open stream: No such file or directory in G:\wamp\www\test\includes\core.inc.php on line 7

(!) Warning: include_once(): Failed opening '../../connectdb.php' for inclusion (include_path='.;C:\php\pear') in G:\wamp\www\test\includes\core.inc.php on line 7

In order to dinamically change the included paths what is the best practice? Please help me on my problem. Thank you.

Community
  • 1
  • 1
xnote
  • 89
  • 2
  • 9
  • 4
    Start relative paths with the directory of the current file, i.e. `require_once dirname(__FILE__) . '/relative/path/to.file.php';` – DCoder Jan 23 '13 at 08:37
  • Give path relative to your file you are editing.. – Deadlock Jan 23 '13 at 08:38
  • 1
    This is a well-defined problem, with the quick-tip to use `__FILE__` and/or `__DIR__` to get the path of your file, not of your `include_path`. There are a lot of questions, for instance, the answer on this seems quite good, though the question itself is a bit long-ish. Just read the accepted answer: http://stackoverflow.com/questions/3099357/php-why-such-weird-behaviour-when-i-include-a-file-from-another-directory-which?rq=1 – Nanne Jan 23 '13 at 08:39
  • I have the following result: `Warning: require_once(G:\wamp\www\testincludes/core.inc.php): failed to open stream: No such file or directory in G:\wamp\www\test\index.php on line 13 `Back slash and forward slashes ... – xnote Jan 23 '13 at 09:08

3 Answers3

4

To avoid such problems, you may use PHP magic constant __DIR__ which will be replaced by current file's directory.

For example:

include_once(__DIR__ . '/relative/path/to/file-to-include.php'); 

Note that __DIR__ is only available for PHP 5.3+. Below that version you can replace __DIR__ by dirname(__FILE__)

BTW, autoloading is a good practice to avoid includes mess.

Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113
  • 1
    It be usefull even "dirname(__FILE__)" – ExoticSeagull Jan 23 '13 at 08:49
  • I have the following result: `Warning: require_once(G:\wamp\www\testincludes/core.inc.php): failed to open stream: No such file or directory in G:\wamp\www\test\index.php on line 13` Back slash and forward slashes ... – xnote Jan 23 '13 at 08:54
  • 2
    Forward/backward slashes mix should not be a problem for PHP on Windows (quoting the manual: `On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/).`). The problem is not on file `core.inc.php` but on file `index.php` which seem to not exist in `G:\wamp\www\test`. Please read carefully the error messages you get. – Maxime Pacary Jan 23 '13 at 10:11
  • Let me try again and I will publish the answer right away... – xnote Jan 23 '13 at 10:17
  • Cheers! Thank you Frosty. Now it worked. Your this comment `Forward/backward slashes mix should not be a problem for PHP on Windows` really helped me. Otherwise \ and / confused me and stopped me to try again. Thank you one more time. – xnote Jan 23 '13 at 10:23
1

included file in index.php : include_once ('./includes/core.inc.php');

Ivan Ferić
  • 4,725
  • 11
  • 37
  • 47
Kai
  • 352
  • 1
  • 9
0

Your relative path in index.php pointing to the core.inc.php is simply wrong.

You are saying: "From the location of index.php, on directory up, and there it is." But there is no file "core.inc.php" in the "www" directory.

The correct path is "includes/core.inc.php": From the current directory "www/test" one directory up into "includes" and there it is.

Any mentions of using __DIR__ or __FILE__ magic constants will also work, but this is a different approach: It will create an absolute path to the file by using relative additions. It works the same, but looks more complicated.

If you are using classes, try to implement autoloading. Then you do not need to include files explicitly, you simply use the classes.

Sven
  • 69,403
  • 10
  • 107
  • 109
  • Can you hint me what classes I should use. __FILE__ actually shows directory where the file is located with backslashes like `G:\wamp\www\test\includes\core.inc.php`. However include function needs forward slashed paths like `/includes/core.inc.php`. When you append both I think it will currupt. – xnote Jan 23 '13 at 09:20
  • Sorry the path is typing mistake. Of course I have checked the included path was exactly as you said. `include_once ('indcludes/core.inc.php');` check I have corrected – xnote Jan 23 '13 at 09:31
  • PHP does not care about slashes being forward or backward - it automatically converts them to the ones that are needed on the platform it runs on. `include` only needs the path (relative or absolute) and filename. You can even mix them when concatenating the string from `__DIR__` and the last part. The only thing to be sure: Do not omit a slash! The result from `__DIR__` does not end with a slash. – Sven Jan 23 '13 at 16:40