Using PHP Includes
Matthew Grdinic  |  April 13, 2008  |  2 Comments
 

For the sake of completeness, let's take a quick look at PHP include basics. We'll start simple and work our way up the complexity ladder.

Simple
In the most basic form, you include a file in the same directory as the calling file like this:

<?php
include 'file.php';
?>

Innocent enough. But what happens if the file you need is one directory up?

<?php
include '../file.php';
?>

What about two directories up?

<?php
include '../../file.php';
?>

And so on.

Includes In Relative Paths Problem

What about an include that has nested call points, in other words, it could be called from /index.php or /content/stories/story.php, and the file being included in turn calls for a file using an include? A typical example of this would be a Database class that calls a config file for connection information.

Although it would be advisable to simply avoid such practices in the first place and use a bootstrapper architecture, what if you couldn't?

<?php
include "{$_SESSION['physical_path']}{$_SESSION['site_path']}{$_SESSION['sub_path_filesystem']}/directory/file.php";
?>

Here session variables would hold physical path information. This code allows us to find the physical directory path to the file, regardless of where it was called from. More importantly, in this example we actually move from including files from within our web structure to our web server's absolute directory structure.

Thus, the values for our session vars above could be:

$_SESSION['physcial_path'] = "c:\\inetpub\\wwwroot\\";

$_SESSION['site_path'] = "sledxchange";

$_SESSION['sub_path_filesystem'] = "\\wcms";

These variables in varying configurations allow us to point to any file in our sites structure, regardless of where the file was located or called from.

Understanding the Problem

To understand why this need for absolute path arises, let's look at an example.

A Windows server directory may look like:

c:\inetpub\wwwroot\site\

And our web structure would be:

/config/config.php

/classes/Database.php -> include: '../config/config.php';

/index.php -> include: 'classes/Database.php';

/content/stories/story_1.php -> include: '../../classes/Database.php';

In this scenario, index.php would work fine, a call to story_1.php would fail. Why?

When you include a file, the included file inherits the calling file's location. In our example above, story_1.php resides in /stories/, this means Database.php's call to ../config/config.php looks in /content, not /config.

Thus, we can use absolute paths to solve this problem.

Two Solutions

To fix the Database.php file, we'd rewrite the include as:

<?php
session_start
();
$_SESSION['physical_path'] = 'C:\\inetpub\wwwroot\\';
$_SESSION['site_path'] = "tech";
include 
$_SESSION['physical_path'] . $_SESSION['site_path'] . '\\config\\config.php';
?>

You could always create constants using the define syntax for the two session vars for a bit cleaner code.

You could also do this if you knew the path of the file you needed to include and it's relative location to the second include:

<?php
$dir 
dirname(__FILE__);
$path str_replace('classes''config\config.php'$dir);
include 
$path;
?>

Here we use the magic constant __FILE__ and replace the known file location of the first include with the known location of the second.

It should be noted that there are other clever ways to avoid the relative path problem, such as using the php.ini directive auto_prepend_file and .htaccess, described here, used in conjunction with constants.


Finally, I'd point out that this behavior is totally different than what you may used to with HTML. In HTML, you can always point to an absolute web path like so:

img src="/welcome.png" mce_src="/welcome.png"

It doesn't matter if that code's in Database.php or index.php, the image will always be located by the web server because it always knows where root is. Not so with PHP.

 
 

Rate Using PHP Includes

Not Rated stars Ave. rating: Not Rated from 0 votes.
  
ADVERTISEMENT

Visitor Comments »

K. Will
May 18th 2008 - 9:49PM
This is a great article because it shows you how to do something in an effective way!
 
Steve Smith
May 18th 2008 - 10:09PM
This seems like a great way to save time when inputting new variables into a stream packet. I will use this often!
 
 
Submit a comment:
name:
(15 chars max)
comment:

Resources
Sample Link → Visit Link
This is what can happen when high powered discharges reach the surface of the earth.