note 32682 deleted from function.realpath by aidan

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

« previous php.notes (#75610) next »