PHP Tip of the day: Just say ‘no’ to absolute file paths

When dealing with file paths in your scripts, it’s best to not specify the absolute path. Keep it simple and specify a relative path. What’s the difference? Check out the example below:

Example: In your index.php file you are using the php include() statement to include a file named “includeme.php” located in the “includes” directory. Two methods of doing this are found below:

Method 1: Relative paths: include(“includes/includeme.php”);
Method 2: Absolute paths: include(“/var/www/html/project/trunk/includes/includeme.php”);

Notice how the first include() statement refers to a file in the “includes” directory, one directory down from the file calling it (in this case index.php). In the second, the include() statement has the entire path to the file right from root.

Why choose option 1 over option 2? Well …

#1. It makes server management easier, faster, and cheaper.

During the lifetime of a web server, the web files may need to be moved and/or directories renamed. Imagine what would happen to your website if you were to rename the “project” directory to “old_project”? You’d have to go through all of your scripts and rename the project directories in the include() statements in all of your scripts.

Using method 1, everything keeps on working with no updates to your scripts.

#2. It makes moving your site to another server easier, faster, and cheaper.

If while maintaining your website you end up switching which VPS company you host with or you change the operating system on your server, you could find yourself in a position where instead of having your web files located in the /var/www directory like most Linux distributions you have them located in the /usr/local/www directory like some BSD installations.

In this case you could use symlinks to forward requests of /var/www to /usr/local/www, change your apache configuration to put your web files to /var/www, or update all of your scripts so that they have the correct path. For now.

All of those things feel like hacks.

Best thing to do is just to code from the start with relative paths, thereby avoiding all of these path problems.

4 thoughts on “PHP Tip of the day: Just say ‘no’ to absolute file paths

  1. What about the following?
    include($incDir1.’relative/path/to/file’);
    include($incDir2.’relative/path/to/file’);
    This will be light as well as portable.

  2. $incDir1 = $_SERVER[‘DOCUMENT_ROOT’];

    Not sure if that’s what your looking for or not…

    I have a bad tendency to do alot of this…

    ../../../path/to/file

    But man does it ever make it confusing when you start to see cross eyed…

  3. yea, I prefer not to use $var to define locations of included files, simply because, that’s how RFI attacks are administered, register_globals off is usually default, which is good, but i always seem to find a reason to use f_open, so ya, simply using the relative method John explained has always seemed to do just fine…

Comments are closed.