Search code examples
phpincluderelative-path

php nested include behavior


In many places in my code, I do things like:

file1.php:
<?php
include('../file2.php');

file2.php:
<?php
include('anotherdirectory/file3.php');

Depending on the server or settings I try this on, it either sets the relative paths from the "includer" or from the "includee". This is really confusing. So file1 might try to include "../anotherdirectory/file3.php" or it might try "anotherdirectory/file3.php".

What settings dictate this behavior? I want to have control over this...


Solution

  • In cases when I need to use relative paths I use the following syntax:

    include (realpath(dirname(__FILE__)."/another_folder/myfile.php"));