note 43733 deleted from function.realpath by aidan

From: Date: Sun, 29 Aug 2004 21:49:19 +0000
Subject: note 43733 deleted from function.realpath by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-75600@lists.php.net to get a copy of this message
Note Submitter: kangaroo232002 at yahoo dot co dot uk ---- 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.

« previous php.notes (#75600) next »