Return to Snippet

Revision: 15320
at June 30, 2009 15:55 by blackhole12


Initial Code
$relpath = "";
$tempvar_relpathdir = explode("/",dirname($_SERVER['PHP_SELF']));
for($i=count($tempvar_relpathdir); $i>0; $i--) if($tempvar_relpathdir[$i] != '') $relpath .= "../";

Initial URL
www.blackspherestudios.net

Initial Description
There's another snippet on here that always comes up on google for finding the relative path to the root, but it doesn't work very well. This version will work from the root, from any subdirectory, and with malformed path names such as www.example.com/////dir////path///file.php

What I have here is designed to be pasted at the top of a php file, but you can also stick it inside a function and return $relpath.

Initial Title
Get Relative Path to Root Directory

Initial Tags
php

Initial Language
PHP