Directory Paths in PHP

July 21, 2004 at 3:09 pm Leave a comment

The way directory paths work in PHP isn’t quite the way I had anticipated… (Until recently, I accessed all include files with the full path, so this wasn’t an issue.)

Let’s say you have the following directory/file structure:

/index.php
/config/config.inc
/includes/auth.php

Your index file in the root directory contains this statement:
include_once(“./includes/auth.php”);

and your auth.php file contains:
include_once(“../config/config.inc”);

In this situation, if you access the auth.php file directly via a web browser, as in “http://mysite.com/includes/auth.php”, it will work as expected, loading the config.inc file and running happily.

However, if you access the index.php file, you will get error messages indicating that the config.inc file can not be found.

Initially, this confused me… I would have thought that the path you use in any included file would be from the location of that file, but it appears that PHP uses the location of the calling file… In this case, the root… So, it was attempting to go up a level, then look for a config directory that didn’t exist…

So, just keep that in mind when designing your directory structure and performing includes, and you’ll be fine…

Advertisements

Entry filed under: PHP.

Build your own Database Driven Website using PHP and MySQL, 2nd ed. My Programming History

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

July 2004
S M T W T F S
    Aug »
 123
45678910
11121314151617
18192021222324
25262728293031

Most Recent Posts


%d bloggers like this: