note 91208 modified in function.dirname by danbrown

From: Date: Sat, 30 May 2009 02:36:43 +0000
Subject: note 91208 modified in function.dirname by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156173@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: <?php 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: <?php require_once( str_replace('//','/',dirname(__FILE__).'/') .'../parent.php'); ?> to include a file that is in the same directory: <?php require_once( str_replace('//','/',dirname(__FILE__).'/') .'neighbor.php'); ?> to include a file that is in a subdirectory: <?php 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. --was-- 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. http://php.net/manual/en/function.dirname.php

« previous php.notes (#156173) next »