note 31301 added to function.realpath
| From: | iceboxman at iceboxman dot com | Date: | Wed, 16 Apr 2003 18:43:03 +0000 |
| Subject: | note 31301 added to function.realpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-46702@lists.php.net to get a copy of this message | ||
OK, after weeks of research and coding and testing, I finally have the "absolute path"
answer everybody has been looking for. These functions have been tested and work in both UNIX and
Windows environments.
Here's what they do:
mapPath:
Takes a file and figures out the relative path from the document requesting it.
basePath:
Takes the output of mapPath and gives you the document root folder
absolutePath:
Takes a file and uses the previous functions to append the base path to it. Then if it's in a
windows environment (where realpath would not return a folder starting with /, it changes the
forward slashes to back slashes.
If you want an absolute path, just use
function mapPath($file) {
if (substr($file,0,1)=="/"){
$file = substr($file,1);
}
$currentPath = dirname($_SERVER["SCRIPT_NAME"]);
if($currentPath=="/")
$Path = "./";
else
$Path = str_repeat("../", substr_count($currentPath, "/"));
return $Path.$file;
}
function basePath()
{
return realpath(mapPath("/"));
}
function absolutePath($file)
{
$file = basePath().$file;
if(substr(basePath(), 0, 1) != "/")
$file = str_replace('/', '\\', $file);
return $file;
}
--
http://www.php.net/manual/en/function.realpath.php
http://master.php.net/manage/user-notes.php?action=edit+31301
http://master.php.net/manage/user-notes.php?action=delete+31301
http://master.php.net/manage/user-notes.php?action=reject+31301