DevTek.Org 
Your PHP Solutions Resource 









Building a dynamic path to the document root for the include function

I like to build modular sites, always have, always will. We used to use frames to set up navigation links and headers and footers but there were so many issues with doing that. Then along came Server Side Includes (SSI) which allowed people to include those files at runtime into the page. Life was good.

PHP has this functionality, but one of the seriously frustrating things about it is the lack of being able to include files relative to the website root directory. This means you have to use the ../ syntax to back up a folder, sometimes multiple times, and then drill down through another folder.

For explanation purposes, here's what I mean:

The folder where your website pages reside for Site1 on the server.
C:\Apache\htdocs\site1

In that folder is your index page some sub-folders containing groups of related pages, and also an includes folder.

              
C:\Apache\htdocs\site1
  includes
    site.css
    db_connect.php
  folder2
    subfolder2-1
      my_database_page.php
    subfolder2-2
  folder3
  folder4
  index.php
              

The includes/site.css is used for every page on the website, and includes/db_connect.php is used on multiple pages throughout the site where you need database connections.

Using the page C:\Apache\htdocs\site1\folder2\subfolder2-1\my_database_page.php, and wanting to include the includes/db_connect.php page, you'd have to do it as follows:

include('../../includes/db_connect.php');

Now, that's no big deal to do, but what if you decide to move the my_database_page.php into folder3? You'd have to remember to take out one set of ../ in your include statements.

Well, after much searching the PHP website, I decided to write my own function to dynamically put in the right number of ../ so that I could move the pages anywhere without having to fix them, and this is what I ended up with.

<?php
function dynRoot()
{
  
$levels substr_count($_SERVER['PHP_SELF'],"/");

  for (
$i=0$i $levels 1$i++)
  {
    
$relativeDir .= "../";
  }

  return 
$relativeDir;
}   
?>


Basically what this is doing is using the PHP function substr_count() to count the number of slashes in the path of the file which this function is called from. So, for instance, the my_database_page.php file would return: /folder2/subfolder2-1/my_database_page.php via the $_SERVER['PHP_SELF'] variable. In this example, there are three slashes, so the number three gets stored in the variable $levels. Since the slash at the front was counted too, we're subtracting one in the for loop condition.

Then we're building a concatenated string with as many ../ that are needed and then returning that string to where it was called with the include() statement such as:

include(dynRoot() . 'includes/db_connect.php')

What we're looking at here is that the dynRoot() function returns (in our example) ../../ and we're concatenating the includes/db_connect.php to it. It'll basically end up being this: include('../../includes/db_connect.php'), and now we have a successfully included db_connect.php file.

Well, that's how you do it. Pretty simple huh? FYI, this is used on almost every single page on this site.

The only caveat is that you have to put the dynRoot() function on every single page you want to do dynamic includes in.



Copyright 2015
All rights reserved.
Powered by PHP   Powered by PSPad   Powered by MySQL   Last Modified
May 15, 2012 @ 12:51