note 97331 added to function.include

From: Date: Thu, 15 Apr 2010 02:18:04 +0000
Subject: note 97331 added to function.include
Groups: php.notes 
Request: Send a blank email to php-notes+get-168246@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: 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. ---- Server IP: 216.235.13.26 Probable Submitter: 64.114.26.193 ---- Manual Page -- http://www.php.net/manual/en/function.include.php Edit -- https://master.php.net/note/edit/97331 Del: integrated -- https://master.php.net/note/delete/97331/integrated Del: useless -- https://master.php.net/note/delete/97331/useless Del: bad code -- https://master.php.net/note/delete/97331/bad+code Del: spam -- https://master.php.net/note/delete/97331/spam Del: non-english -- https://master.php.net/note/delete/97331/non-english Del: in docs -- https://master.php.net/note/delete/97331/in+docs Del: other reasons-- https://master.php.net/note/delete/97331 Reject -- https://master.php.net/note/reject/97331 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#168246) next »