Get Relative Path to Root Directory


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. $relpath = "";
  2. $tempvar_relpathdir = explode("/",dirname($_SERVER['PHP_SELF']));
  3. for($i=count($tempvar_relpathdir); $i>0; $i--) if($tempvar_relpathdir[$i] != '') $relpath .= "../";

URL: www.blackspherestudios.net

Report this snippet


Comments


You need to login to view comments.