note 32682 added to function.realpath
| From: | jruiz at lmental dot com | Date: | Wed, 04 Jun 2003 11:17:36 +0000 |
| Subject: | note 32682 added to function.realpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-49708@lists.php.net to get a copy of this message | ||
Realpath is not useful for resolving a path against a base path.
Consider the ACTION from a FORM in a HTML page. The ACTION is a URL that can contain relative path
information. How do you resolve an ACTION URL from the basepath of the script? The answer is
"resolvePath" you can find below.
Example:
The page at "/forms/newuser/myform.php" contains a FORM with an ACTION like
"../scripts/adduser.php".
This script, called like:
$realPath = resolvePath( "/forms/newuser/myform.php", "../scripts/adduser.php");
Will return: "/forms/scripts/adduser.php", which is the "realpath" comparing the
two URLS.
I hope this will be useful.
Jose Miguel
jruiz [at] lmental [dot] com
//-----------------------------------------------------
function resolvePath( $orig, $dest )
{
// 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);
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 );
$count--;
}
if ($tblPath[$i] == "..") {
array_splice( $tblPath, $i-1, 2 );
$i--;
$count-=2;
}
}
return "/".implode("/", $tblPath);
}
//-----------------------------------------------------
----
Manual Page -- http://www.php.net/manual/en/function.realpath.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+32682
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+32682&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+32682&report=yes