Jump to content

include_path : is there an elegant solution?


Zearin

Recommended Posts

I'm starting a new website project, and I'm following a naming convention that is strictly adhered to.  My goal is to configure [tt]include_path[/tt] so that I can simply type a filename and it will be included. 

The scheme is this:
[list]
[*]the website root has a folder named [tt]/includes[/tt]
[*]each subdirectory has its own folder named [tt]/includes[/tt]
[/list]

[b]Directory Structure[/b]
[pre]
[b]+ site_root[/b]
    index.php
   
    [b]+ includes[/b]
          header.php
          footer.php

    [b]+ sub_dir[/b]
          index.php

          [b]+ includes[/b]
              header.php
              footer.php
   
[/pre]

[b]Desired Functionality[/b]
Let's say I type [tt]http://www.site_root.com/sub_dir/[/tt] into my browser...

[list]
[*][tt]/sub_dir/index.php[/tt] begins to load
[*]it wants to include [tt]header.php[/tt] from [tt]/sub_dir/includes[/tt]
[*]the included file ([tt]/sub_dir/includes/header.php[/tt] in turn wants to include the [tt]header.php[/tt] from [tt]site_root/includes[/tt]
[/list]

The goal of the scheme is to have a root header which can optionally be appended with headers for specific sections of the website.

I've played around quite a bit with trying to get the include_path to behave the way I want it to, but it seems that I can only get it to include the [tt]header.php[/tt] from the root level or nothing at all.  Does anyone know how to set up this configuration?
Link to comment
Share on other sites

The path is relative to the included file, not the including file.  That means you can do

[code=php:0]include('../header.php');[/code] to include one level up.

I think your best bet is a little function to walk up the path and try including files for you.  That function will need to be in a fixed location so it can be included before the other including takes place.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.