0

I have a file test.php in the folder myfolder. myfolder also contains another folder called inner.

Both myfolder and inner contain a file called msg.php. The entire layout looks like this:

  • myfolder
    • test.php
    • inner
      • msg.php
    • msg.php

In test.php, I've set the include_path to ./inner and included the file msg.php:

<?php
error_reporting(E_ALL | E_STRICT);
ini_set("include_path", "./inner");
echo ini_get('include_path'); // shows ./inner
include("msg.php"); // outputs hello from myfolder/inner/msg.php
?>

However, if I modify the working directory to ./inner, myfolder/msg.php will be included instead of myfolder/inner/msg.php:

<?php
error_reporting(E_ALL | E_STRICT);
ini_set("include_path", "./inner");
echo ini_get('include_path'); // shows ./inner
chdir('./inner');
include("msg.php"); // outputs hello from myfolder/msg.php
?>

Shouldn't the second piece of code include myfolder/inner/msg.php instead of myfolder/msg.php?

Pacerier
  • 86,231
  • 106
  • 366
  • 634
  • side note.. your overwriting your include path completely when you do `ini_set("include_path", "./inner");` always be sure to also include the current include path when adding a new include path – DevZer0 Jul 16 '13 at 12:17
  • @DevZer0, yes I am overwriting the include path. Thus the second piece of code should include myfolder/inner/msg.php instead of myfolder/msg.php right? – Pacerier Jul 16 '13 at 12:18
  • 1
    In the second example, you chdir to `./inner` then tell it to set the `include_path` to a dir `./inner` which doesn't exist. You're actually telling it to set the include dir to `/inner/inner`. Try setting the include dir to `/inner` instead of `./inner` – crush Jul 16 '13 at 12:20
  • yes it should include `./inner` – DevZer0 Jul 16 '13 at 12:20
  • Since the include_path in your 2nd example doesn't exist the include will trigger this behavior: "If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing." – Ma3x Jul 16 '13 at 12:23
  • @Akam, see the changes. – Pacerier Jul 16 '13 at 12:24
  • 1
    @Pacerier: No, it checks in the script's calling dir first. – Ma3x Jul 16 '13 at 12:25

2 Answers2

3

Let's first take a look at your directory path syntax.

./inner

This is saying, look in the current directory (./) for a directory called inner.

Before you set the include_path to ./inner, though, you change the current working directory to ./inner, so now you are effectively looking for /myfolder/inner/inner/msg.php.

Let's take a look at your code again.

//Current working directory is /myfolder

//Here you change the current working directory to /myfolder/inner
chdir('./inner');

//Here you set the include_path to the directory inner in the current working directory, or /myfolder/inner/inner
ini_set("include_path", "./inner");

//You echo out the explicit value of include_path, which, of course, is ./inner
echo ini_get('include_path'); // shows ./inner

//include fails to find msg.php in /myfolder/inner/inner so it looks in the scripts directory, which is /myfolder/msg.php.
include("msg.php"); // outputs hello from myfolder/msg.php

Check the documentation which states the following about what happens if include() can't find the file referenced in the provided path:

include will finally check in the calling script's own directory and the current working directory before failing.

You should try setting the include_path to /myfolder/inner or, if /myfolder is actually your root, then you could just set it to /inner. Notice the omission of the . which means current working directory. Just using a / means look in the root directory.

crush
  • 16,713
  • 9
  • 59
  • 100
1

Chances are your path is wrong, take of the "./" from the path. From what I see it look like you are coming out of the myfolder and then looking for a inner outwith it.

Check the docs for the include function: http://php.net/manual/en/function.include.php

include will finally check in the calling script's own directory and the current working directory before failing

  • 1
    The quote in your answer is why it still finds `/myfolder/msg.php` after failing to find `/myfolder/inner/inner/msg.php` – crush Jul 16 '13 at 12:27