Zearin Posted December 14, 2006 Share Posted December 14, 2006 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? Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted December 14, 2006 Share Posted December 14, 2006 use $_SERVER['DOCUMENT_ROOT'] Quote Link to comment Share on other sites More sharing options...
Zearin Posted December 14, 2006 Author Share Posted December 14, 2006 I know about the $_SERVER variables, but thank you for that. :)What I'm having trouble with is getting it to search up the folder hierarchy, and allowing for an included header.php to include another header.php from one level up. Quote Link to comment Share on other sites More sharing options...
Zearin Posted December 15, 2006 Author Share Posted December 15, 2006 So... does anyone have any ideas? Also, when [tt]/sub_dir/includes/header.php[/tt] calls include itself, is the path relative to the page in the user's browser or to the header.php file? Quote Link to comment Share on other sites More sharing options...
drifter Posted December 15, 2006 Share Posted December 15, 2006 I use DEFINE in my config file to define the paths of all my include, my data, etc - that way even if I deside to change stucture, I can move it.include(MYINCLUDES . "header.php"); Quote Link to comment Share on other sites More sharing options...
Albright Posted December 15, 2006 Share Posted December 15, 2006 Have you tried include("../../includes/header.php") ?Suggestion: use require_once() instead. That way you get a visible error if the file is missing, and your script won't include the file twice if you accidentally try to do so. Quote Link to comment Share on other sites More sharing options...
btherl Posted December 15, 2006 Share Posted December 15, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.