Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling include from an included file

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

like image 260
Hailwood Avatar asked Jul 26 '10 10:07

Hailwood


People also ask

How do I call a function from an include PHP file?

To call a function from another file in PHP, you need to import the file where the function is defined before calling it. You can import a PHP file by using the require statement.

What are include () and require () functions?

The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

What is __ DIR __ in PHP?

The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.

What is the purpose of the include command?

INCLUDE includes a file of commands in a session. INCLUDE is especially useful for including a long series of data definition statements or transformations. Another use for INCLUDE is to set up a library of commonly used commands and include them in the command sequence as they are needed.


2 Answers

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.

like image 129
Pascal MARTIN Avatar answered Sep 25 '22 21:09

Pascal MARTIN


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';

like image 31
ut9081 Avatar answered Sep 23 '22 21:09

ut9081