disc@puff:~/php$ ls
a.php data include
disc@puff:~/php$ tree
.
├── a.php
├── data
│ └── d.php
└── include
├── b.php
└── c.php
2 directories, 4
paths are always relative to the script which got called. in your example c.php is loaded because "." (current directory) is always in the include_path.
to fix this you can use dirname(__FILE__)
to always know the directory of the file itself. (the file in which you write FILE)
or you can use dirname($_SERVER['SCRIPT_FILENAME'])
to alwys get the directory of the caling script.
As you're starting with a.php
, you should define the include directories in a.php
:
define('MY_INCLUDES', dirname(__FILE__) . '/include/');
define('MY_DATA', dirname(__FILE__) . '/data/');
Afterwards include the files with absolute paths:
include(MY_INCLUDES . 'b.php');
include(MY_DATA . 'c.php');