31

So, examining this directory structure

  • /include_one.php
  • /include_two.php
  • /directory/main_file.php

Assume that I am in /directory/main_file.php and I call include('../include_one.php'); inside of include_one.php, to include include_two.php. Do I need to call include('include_two.php); or include('../include_two.php');?

So my question is: When including a file, is the 'relative include path' shifted to the included file, or does it remain at the main including file?

I am aware that the best alternative would be to have a config.php which contains the root_path, however this is not possible at this stage.


update:
So, im not sure who is right, as here is my test

directory structure

/include.php
/start/start.php
/folder1/includeone.php
/folder1/folder2/includetwo.php

and here is the contents of each file

start.php

<?php 
  echo 'including ../include.php<br />';
  include('../include.php');
?>

include.php

<?php 
  echo 'including folder1/includeone.php<br />';
  include('folder1/includeone.php');
?>

includeone.php

<?php 
  echo 'including folder2/includetwo.php<br />';
  include('folder2/includetwo.php');
?>

includetwo.php

<?php 
  echo 'done<br />';
?>

and the output is

including ../include.php
including folder1/includeone.php
including folder2/includetwo.php
done

Hailwood
  • 89,623
  • 107
  • 270
  • 423
  • still no answer to my update... – Hailwood Jul 26 '10 at 15:28
  • Hi. I believe this to depend on the system running the code. I am currently working in a 4 years-old project, which is run across different OS and settings. Somehow, everytime I clone the code, I have to configure everything again, while a coworker of mine can simply run it everytime he clones it to his machine. I'm currently in search for a way to make PHP shift the "relative include path" everytime it changes from file to file. – Gui Imamura Sep 18 '15 at 19:57

4 Answers4

63

The "relative include path" is not shifted to the included file... Which means that using relative paths generally ends badly.

A better solution, that I use almost all the time, is to always work with absolute paths -- and you can mix relatives and absolute paths using __DIR__, to get the directory that contains the file where this is written.


For example, in include_one.php, you'd use :

require_once __DIR__ . '/include_two.php';

To include the include_two.php file that's in the same directory as include_one.php.


And, in main_file.php, you'd use :

require_once __DIR__ . '/../include_one.php';

To include the include_one.php file that's one directory up.


With that, your includes will work, no matter from which file they are called.

Donnie Ashok
  • 1,355
  • 1
  • 11
  • 26
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
3

The include path is relative to the first file in the include chain.

A good way to ensure the correct include path is to always include from the document root.

This is done like this:

include $_SERVER['DOCUMENT_ROOT'] . '/folder1/folder2/includetwo.php';

ut9081
  • 787
  • 6
  • 11
2

Two demos of how include works in PHP

Im 2010 the author has wrote in his question:

So, im not sure who is right, as here is my test ...

and also in the comment:

still no answer to my update...

So I suggest my tests to understand how PHP includes work. The results were the same for versions 5 and 7.

Test 1

Files and directories:

  1. /index.php
    <?php
    include 'subdirectory/index.php';
  1. /subdirectory/index.php
    <?php
    include 'test.php';
  1. /subdirectory/test.php
    <?php
    echo __FILE__;

The output:

/subdirectory/test.php

This test work well, because there is one and only one candidate for every include.

This requirement is also met for the test from the updated question. This is why it works well too.

Test 2

Let's copy test.php to the main root. So the new project structure is as follows:

/index.php
/test.php
/subdirectory/index.php
/subdirectory/test.php

Now the output becomes different::

/test.php

Second test has another result, because there are two candidates for the instruction include 'test.php'; and PHP chose by itself which file to include.

This is why the practice suggested by the author of the accepted answer saves the project from surprises.

Gleb Kemarsky
  • 10,160
  • 7
  • 43
  • 68
1

I am using this code:

if(!isset($TO_HOME_DIR)) $TO_HOME_DIR="../"; 

And I include a file:

include_once($TO_HOME_DIR."common/include_one.php");

With if(!isset($TO_HOME_DIR)), it's not important how much you include a file into included file into included file into includ..... Only first file's -and main file's- $TO_HOME_DIR declaration is used.

Second advantage of this approach is, if you change directory of file, you only need to change one line of code; $TO_HOME_DIR declaration. :)

Kuvalya
  • 1,094
  • 1
  • 15
  • 26