note 91208 added to function.dirname

From: Date: Fri, 29 May 2009 20:33:07 +0000
Subject: note 91208 added to function.dirname
Groups: php.notes 
Request: Send a blank email to php-notes+get-156168@lists.php.net to get a copy of this message
A key problem to hierarchical include trees is that PHP processes include paths relative to the original file, not the current including file. A solution to that, is to prefix all include paths with: str_replace('//','/',dirname(__FILE__) this will generate a base path relative to the current file, which will then allow an include behavior similar to C/C++. thus, to include a file that is 1 in the parent directory: require_once( str_replace('//','/',dirname(__FILE__).'/') .'../parent.php'); to include a file that is in the same directory: require_once( str_replace('//','/',dirname(__FILE__).'/') .'neighbor.php'); to include a file that is in a subdirectory: require_once( str_replace('//','/',dirname(__FILE__).'/') .'folder/sub.php'); Notice that all paths we reference must NOT begin with a /, and must be relative to the current file, in order to concatenate correctly. ---- Server IP: 209.41.74.194 Probable Submitter: 15.203.233.75 ---- Manual Page -- http://www.php.net/manual/en/function.dirname.php Edit -- https://master.php.net/note/edit/91208 Del: integrated -- https://master.php.net/note/delete/91208/integrated Del: useless -- https://master.php.net/note/delete/91208/useless Del: bad code -- https://master.php.net/note/delete/91208/bad+code Del: spam -- https://master.php.net/note/delete/91208/spam Del: non-english -- https://master.php.net/note/delete/91208/non-english Del: in docs -- https://master.php.net/note/delete/91208/in+docs Del: other reasons-- https://master.php.net/note/delete/91208 Reject -- https://master.php.net/note/reject/91208 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#156168) next »