1

So I set up my include files, such as header.php, nav.php, and footer.php in a /inc folder.

Then I include the files in my pages. But if I have another folder, for example wiki where i'd like to include those files onto the pages of wiki, the file paths for the includes break.

Example: I have included this in a page from the wiki directory.

<?php include_once('../inc/header.php'); ?>

I have two other includes in the header.php file, but since I'm including them in a different directory, both includes break, unless I go and append a ../ in the include path.

I'm wondering what is the best way to include files within different directories and not having to go back and fix the path?

devs
  • 541
  • 2
  • 13
  • 27

8 Answers8

3

Create constants using realpath in the initial file and then reference them using easy to read and manage but now absolute paths.

You can see an example here:

How can I get the "application root" of my URL from PHP?

I'd also suggest testing for the constant definition before defining.

Community
  • 1
  • 1
Matt Whipple
  • 7,034
  • 1
  • 23
  • 34
1

The simplest solution? Use absolute paths.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
1

If the files are in directories of the same level, the relative pathes (with ../) will be the same. If you have one within another, you will need to have the ../ on the beginning of the one in the sub-directory. The only way around changing the paths is to create a link as a sub-directory representing the main directory, although this can have dangerous consequences relating to recursive processes.

btw, include_once means that this operation is "optional", meaning the program will continue to execute. If you require the files included, use require_once, and the program will break if it's not there.

Jim Lutz
  • 139
  • 6
0

Include them like this

require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'inc'.DIRECTORY_SEPARATOR.'header.php');

This could work!

rsz
  • 1,141
  • 1
  • 18
  • 38
0

I like using this method:

define('ROOT_DIR', dirname(__FILE__) . '/'); // Defined on the index or in the bootstrap
include(ROOT_DIR . 'subfolder/fileName.php');
Matt Whitehead
  • 1,743
  • 3
  • 19
  • 34
0

If you use PHP's native set_include_path to add an include folder you can then include files just by their name.

<?php
/**
 * The path to your /inc/ folder.
 */
$path = '/absolute/path/to/inc/';

/**
 * get_include_path retrieve's the current include path. 
 * It's a bad idea to over write the entire include path
 * The PATH_SEPARATOR constant is set per operating system
 */
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

/**
 * You can now include any files in /inc/ from any directory with no path
 */
include_once('header.php');
?>
thelastshadow
  • 3,406
  • 3
  • 33
  • 36
0

$_SERVER['DOCUMENT_ROOT'] gives you the DocumentRoot of your webserver, e.g. Apache. Then you can take that as a stable datum and map from there as that won't vary.

include_once($_SERVER['DOCUMENT_ROOT'].'/inc/header.php');
Peter Oram
  • 6,213
  • 2
  • 27
  • 40
0

I found that I had to do str_replace("\\","/",$_SERVER['DOCUMENT_ROOT']) when running on IIS if that helps.

philn5d
  • 636
  • 7
  • 12