devs
devs

Reputation: 551

including files from different directories

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?

Upvotes: 1

Views: 483

Answers (8)

philn5d
philn5d

Reputation: 634

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

Upvotes: 0

Peter Oram
Peter Oram

Reputation: 6768

$_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');

Upvotes: 0

thelastshadow
thelastshadow

Reputation: 3654

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

Upvotes: 0

Jim Lutz
Jim Lutz

Reputation: 139

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.

Upvotes: 1

Matt Whitehead
Matt Whitehead

Reputation: 1811

I like using this method:

define('ROOT_DIR', dirname(__FILE__) . '/'); // Defined on the index or in the bootstrap
include(ROOT_DIR . 'subfolder/fileName.php');

Upvotes: 0

Matt Whipple
Matt Whipple

Reputation: 7134

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.

Upvotes: 3

rsz
rsz

Reputation: 1161

Include them like this

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

This could work!

Upvotes: 0

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 175098

The simplest solution? Use absolute paths.

Upvotes: 1

Related Questions