preloaded image preloaded image preloaded image

PHP Directory Listings

Selection and Treatment of the Root Path and of File Directories

So we've all been there as we're learning PHP. We're used to HTML rules and we want to specify a directory or a file in a PHP script relative to the root (commonly desired for a PHP include). And why not? It's very useful. Our URL is http://www.<domain_name>.com so we try this:

include ("/includes/xxxxx.php");

Looks good to me, right? Those of us who are accustomed to client-side development and haven't worked our way too far into the server-side aspect might expect this to include a file named "xxxxx.php" located at http://www.<domain_name>.com/includes/ but when we try this our script grinds to a halt and the file is not included. Errors result and it's no fun at all. What's the deal? This is exactly how we have been doing this in HTML for ages. Why won't PHP get this right?!?!

Well,the problem is that this is no good in PHP. Your PHP root is not the same as your web root because PHP has access to folders above the "www" folder while javascript and HTML do not. What are the options? Well...

  1. We could use relative filepaths rather than defining them from the root
  2. We can find the PHP root in each page, for each include whenever necessary.

Ok, option 1: A potentially messy approach. Here's why... Suppose we have a site, and suppose we have been using relative file paths for our includes from day 1. This made us happy enough. Sure, it was annoying (and a waste of time) to amend the file path for each new subdirectory that we used - especially where templates or SQL transactions are involved - but we're past all of that now. The site is up, and what could possibly go wrong?

Well, suppose we (or our client) find the need to take one of our directories (say, "widgets") and make it a subdirectory of another, perhaps new directory (say, "mythical_products"). Suppose that the widgets directory had several subdirectories of its own. Now we're looking at updating the relative file paths of every single file in each affected folder, even if we're just trying to get to our root directory to find images, css, or even the main index file. This usually just means adding a whole bunch of "../" to file paths, but think about how many times and how many places you would need to do that. You could be at it for weeks and then you would still have to test every single link in every single page to make sure that you got them all. Talk about an exhausting waste of time!

Option 2: Scalable? Yes! Using the built-in variable $_SERVER['DOCUMENT_ROOT'] we can return the full root directory path for PHP (note that there will not be a slash "/" at the end of the returned value so you will need to add one in order to access files in the root directory). This works something like this for a basic include:

include($_SERVER['DOCUMENT_ROOT']."/some_content.php");

This code will faithfully pull up the file "some_content.php" located in our root web directory regardless of what file you place the code into. This can be 2 layers deep in subdirectories or 20 layers deep and it will work just the same even when moved around (as long as "some_content.php" stays where it was).

Another miscellaneous treat is the $_SERVER['PHP_SELF'] variable. This one will return the path of the currently handled file relative to the root. The combination of this variable with the $_SERVER['DOCUMENT_ROOT'] variable can open up some very handy options that allow you to move files between directories, rename directories, or do pretty much anything you want with file paths without any maintenance across your bulk page files.