note 33451 deleted from function.dirname by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 19:08:40 +0000 |
| Subject: | note 33451 deleted from function.dirname by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142275@lists.php.net to get a copy of this message | ||
Note Submitter: degoede at tomaatnet dot nl
----
Easy to have config functionality using dirname function.
inc.config.php:
===============================
/* UNC path to include file, plus trailing slash.
Convert back-slashes to slashes, always, on linux there will be none to replace, but who cares.
Use str_replace, as this is the fastest on simple replacements, according to php.net. */
$unc = str_replace( "\\\\", "/", dirname( __FILE__ ) . "/" ) ;
/* Remove the UNC path to this file from the UNC path to the main php script that included this
file, only keeping any subfolder names, relative to this file's UNC. */
$url = str_replace( $unc, "", $_SERVER["SCRIPT_FILENAME"] ?
$_SERVER["SCRIPT_FILENAME"] : $HTTP_SERVER_VARS["SCRIPT_FILENAME"] ) ;
/* Remove the value obtained from the calling php script's URL, to get the URL to this file.*/
$url = str_replace( $url, "", $_SERVER["SCRIPT_NAME"] ?
$_SERVER["SCRIPT_NAME"] : $HTTP_SERVER_VARS["SCRIPT_NAME"] ) ;
/* Make a general purpose configuration data array. */
$Config["root"]["unc"] = $unc ;
$Config["root"]["url"] = $url ;
$Config["dir"]["images"] = "images/" ;
$Config["dir"]["include"] = "include/" ;
/* use functions to keep the interface simple and, seperate interface from internal functioning. */
function get_fullpath( $type, $dir )
{
global $Config ;
$FullPath = $Config["root"][$type] ;
$FullPath .= $Config["dir"][$dir] ;
return ( $FullPath ) ;
}
function get_unc( $dir, $file )
{
return ( get_fullpath( "unc", $dir ) . $file ) ;
}
function get_url( $dir, $file )
{
return ( get_fullpath( "url", $dir ) . $file ) ;
}
some_file.php:
===============================
include_once ( ../../inc.config.php ) ;
include_once ( get_unc( "include", "inc.some_include.php" ) ) ;
Now you only need to worry about a relative path once.