note 97331 modified in function.include by danbrown

From: Date: Thu, 15 Apr 2010 13:18:08 +0000
Subject: note 97331 modified in function.include by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-168281@lists.php.net to get a copy of this message
It appears to be a common problem for an include file to know the directory level distance from the logical root of a site to the script including the file (what I will call the "includer"). This is required if the include file needs to refer to other files such as images or nested includes and is included from within files at different directory levels. The relative file depth (../ or ../../ etc.) needs to be known. The solutions offered above break with anything which makes the true server root different from the logical root for the site. This includes use of Apache alias directories, directory links or simply the common practice of using a sub-folder under the www directory for testing but not production. The only solution I have found so far is to create an include file at a known directory level which gets included at the beginning of every page in the site. This is common with headers such as a header.inc.php file. At the start of this include file have the following code: <?php session_start(); /* if not already done */ /* replace value below with appropriate header to root distance of this include file */ $header_to_root_distance = 1; $header_dir = dirname(__FILE__); $root_distance = substr_count($header_dir, DIRECTORY_SEPARATOR) - $header_to_root_distance; $includer_distance = substr_count(dirname($_SERVER['SCRIPT_FILENAME']), "/"); $relative_path = str_repeat('../', $includer_distance - $root_distance); $_SESSION['relative_path'] = $relative_path; ?> The session variable "relative_path" will now always contain the correct relative path back the site logical root and may be concatenated with file references so they will work no matter which directory level the main includer file resides at within the site. --was-- It appears to be a common problem for an include file to know the directory level distance from the logical root of a site to the script including the file (what I will call the "includer"). This is required if the include file needs to refer to other files such as images or nested includes and is included from within files at different directory levels. The relative file depth (../ or ../../ etc.) needs to be known. The solutions offered above break with anything which makes the true server root different from the logical root for the site. This includes use of Apache alias directories, directory links or simply the common practice of using a sub-folder under the www directory for testing but not production. The only solution I have found so far is to create an include file at a known directory level which gets included at the beginning of every page in the site. This is common with headers such as a header.inc.php file. At the start of this include file have the following code: session_start(); /* if not already done */ /* replace value below with appropriate header to root distance of this include file */ $header_to_root_distance = 1; $header_dir = dirname(__FILE__); $root_distance = substr_count($header_dir, DIRECTORY_SEPARATOR) - $header_to_root_distance; $includer_distance = substr_count(dirname($_SERVER['SCRIPT_FILENAME']), "/"); $relative_path = str_repeat('../', $includer_distance - $root_distance); $_SESSION['relative_path'] = $relative_path; The session variable "relative_path" will now always contain the correct relative path back the site logical root and may be concatenated with file references so they will work no matter which directory level the main includer file resides at within the site. http://php.net/manual/en/function.include.php

« previous php.notes (#168281) next »