note 33451 added to function.dirname
| From: | degoede at tomaatnet dot nl | Date: | Thu, 26 Jun 2003 12:58:11 +0000 |
| Subject: | note 33451 added to function.dirname | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-51039@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.dirname.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33451
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33451&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33451&report=yes