note 31301 deleted from function.realpath by aidan
| From: | aidan@php.net | Date: | Sun, 29 Aug 2004 21:51:07 +0000 |
| Subject: | note 31301 deleted from function.realpath by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-75612@lists.php.net to get a copy of this message | ||
Note Submitter: iceboxman at iceboxman dot com
----
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;
}