note 43733 added to function.realpath
| From: | kangaroo232002 at yahoo dot co dot uk | Date: | Thu, 01 Jul 2004 17:24:43 +0000 |
| Subject: | note 43733 added to function.realpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-72271@lists.php.net to get a copy of this message | ||
i also got caught into the trap of believing that realpath would magically detect the absolute URL -
possibly because of the "index.php" example on the page...? anyway, if you want to resolve
a URL - the following works quite well and is a mod on "jruiz at lmental dot com's"
example, this one irons out a few previous errors, and probably creates some more.
<?php
function resolvePath($orig, $dest) {
$orig = str_replace("\\","/",$orig);
$urlinfo = parse_url($orig);
$orig = $urlinfo['path'];
// Normalize path
// If $orig is a filename, extract the directory information
if ($orig[strlen($orig)-1] != "/")
$orig = dirname($orig);
// Delete multiple /// in path
$dest = ereg_replace("/+", "/", $dest);
//removes internal links such as "/#hey" or ../#hey or #whatever etc
//leaves other links like index.php#hey
if(eregi("^#[a-z0-9_?#]*|^.+/+#[a-z0-9_?#]*",$dest))
return false;
if (strlen($dest) && ($dest[0] == '/')) {
// If $dest start with /, we have to simulate a root directory
// by not including the original path ($orig)
$tblPath = explode("/", $dest);
}
else {
$tblPath = array_merge( explode("/", $orig), explode("/",$dest));
}
// OK, we now have an array with all posible individual directories,
// including, "." and ".."
$count = count($tblPath);
for ($i=0; $i<$count; $i++) {
if (!strlen($tblPath[$i]) || ($tblPath[$i] == ".")) {
array_splice( $tblPath, $i, 1 );
$i--;
$count--;
}
elseif (@$tblPath[$i] == "..") {
array_splice( $tblPath, $i-1, 2 );
$i-=2;
$count-=2;
}
}
//for use with localhost where parse_url seems to get confused -
// put this on the same line
strstr("http",$urlinfo['host']) ? $scheme = "" :
$scheme = $urlinfo['scheme'] . "://";
$name = "/" . implode("/", $tblPath);
//implode wasnt working properly and sticking a "/" on the end if
//the route is a directory.
$info = pathinfo($name);
if(@!isset($info['extension']))
$name = "$name/";
if(@isset($urlinfo['host']))
return "$scheme" . $urlinfo['host'] . "$name";
else
return false;
}
$path = resolvePath("http://localhost/old_pages/directory/
catalogue.htm","../../work/uni/");
echo "$path";
?>
Apologies for the layout. Accepts (only) a http://domain_name/ (as
seen in $path) in $orig, and then a directory, path, or file in $dest. hope that helps anyway.
ian.
----
Manual Page -- http://www.php.net/manual/en/function.realpath.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43733
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43733&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43733&report=yes
Search -- http://master.php.net/manage/user-notes.php