Re: Relative Url
| From: | K.Bogac Bokeer | Date: | Sat, 03 Apr 2004 07:19:47 +0000 |
| Subject: | Re: Relative Url | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-182240@lists.php.net to get a copy of this message | ||
I'm using this on Apache. You need to include 'inc/globals.php' in your script.
inc/globals.php
<?
require_once('functions.php');
define('RELPATH', GetRelativePath());
?>
inc/functions.php
function GetRelativePath() {
$RELPATH = str_replace(UpDirectory(__FILE__), '', dir_name($_SERVER['SCRIPT_NAME]));
$RELPATH = substr_count($RELPATH, '/');
if ( $RELPATH>0 )
$RELPATH = str_repeat('../', $RELPATH);
else
$RELPATH = '/';
return ConvertSlashes($RELPATH);
}
function UpDirectory($directory, $level=1) {
while ( $level-- > 0 )
$directory = dirname(dirname($directory));
return ConvertSlashes($directory);
}
function ConvertSlashes($st) {
$st = str_replace('\\\\', '/', $st);
$st = str_replace('\\', '/', $st);
return $st;
}
function dir_name($directory) {
return ConvertSlashes(dirname($directory));
}
Chris Thomas wrote:
Is there anyway that i can get a url relative to my server for a script that is being run?? The script is being included in mulitple files and $_SERVER['SCRIPT_NAME'] just gives me the name of the file that is including the other scripts. Also $_SERVER['SCRIPT_FILENAME'] is returning nothing. Thanks Chris