<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Comments on snippet: 'Get relative path to root'</title>
    <description>Snipplr comments feed</description>
    <link>https://ipv6.snipplr.com/</link>
    <lastBuildDate>Thu, 23 Apr 2026 06:55:03 +0000</lastBuildDate>
    <item>
      <title>skcsknathan001 said on 20/Jun/2008</title>
      <link>https://ipv6.snipplr.com/view/2467/get-relative-path-to-root</link>
      <description>&lt;p&gt;&lt;p&gt;Thank you very much.  &lt;/p&gt;

&lt;p&gt;I was not able to get DOCUMENT_ROOT or any other server variables.  All was returning empty.&lt;/p&gt;

&lt;p&gt;I used this and it's working.  I modified&lt;/p&gt;

&lt;p&gt;$path = "";
for($i=count(explode("/",dirname($&lt;em&gt;SERVER['PHP&lt;/em&gt;SELF']))); $i&gt;1; $i--) $path .= "../";
echo $path;&lt;/p&gt;

&lt;p&gt;this works from root folder itself and any level of subfolders&lt;/p&gt;

&lt;p&gt;Thanks again&lt;/p&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 20 Jun 2008 09:48:14 UTC</pubDate>
      <guid>https://ipv6.snipplr.com/view/2467/get-relative-path-to-root</guid>
    </item>
    <item>
      <title>relaxasaurus said on 04/Dec/2008</title>
      <link>https://ipv6.snipplr.com/view/2467/get-relative-path-to-root</link>
      <description>&lt;p&gt;Thanks for the code, though it doesn't seem to work for me.  It always returns "../" whether I'm 1 or 2 directories deep.&lt;/p&gt;</description>
      <pubDate>Thu, 04 Dec 2008 15:58:27 UTC</pubDate>
      <guid>https://ipv6.snipplr.com/view/2467/get-relative-path-to-root</guid>
    </item>
    <item>
      <title>blackhole12 said on 30/Jun/2009</title>
      <link>https://ipv6.snipplr.com/view/2467/get-relative-path-to-root</link>
      <description>&lt;p&gt;This code doesn't work. After about an hour and a half of painful searching and failed techniques, I came up with the following code snippet that DOES work under all circumstances, even when you have malformed paths such as http://www.blah.com////path//to///file.php&#13;
&#13;
$relpath = "";&#13;
$tempvar_relpathdir = explode("/",dirname($_SERVER['PHP_SELF']));&#13;
for($i=count($tempvar_relpathdir); $i&gt;0; $i--) if($tempvar_relpathdir[$i] != '') $relpath .= "../";&#13;
&#13;
Hopefully since this is the first result that comes up on google, this will save the pain of others in the same situation.&lt;/p&gt;</description>
      <pubDate>Tue, 30 Jun 2009 15:49:34 UTC</pubDate>
      <guid>https://ipv6.snipplr.com/view/2467/get-relative-path-to-root</guid>
    </item>
    <item>
      <title>frankyfish said on 17/Mar/2010</title>
      <link>https://ipv6.snipplr.com/view/2467/get-relative-path-to-root</link>
      <description>&lt;p&gt;Thanks for the feedback&lt;/p&gt;</description>
      <pubDate>Wed, 17 Mar 2010 14:02:42 UTC</pubDate>
      <guid>https://ipv6.snipplr.com/view/2467/get-relative-path-to-root</guid>
    </item>
    <item>
      <title>jHulbert said on 28/Mar/2011</title>
      <link>https://ipv6.snipplr.com/view/2467/get-relative-path-to-root</link>
      <description>&lt;p&gt;[code:]&#13;
function spec_root($root=NULL) {&#13;
	$cur_file = $_SERVER["SCRIPT_NAME"];&#13;
    $cur_file = explode('/', $cur_file);&#13;
    $cur_file = $cur_file[count($cur_file) - 1];&#13;
	$tmp = split($root, $_SERVER['REQUEST_URI']);&#13;
	$tmp = str_replace($cur_file, "", $tmp);&#13;
	$tmp = $tmp[1];&#13;
    $tmp = str_replace('\\', '/', $tmp);&#13;
    $tmp = explode('/', $tmp);&#13;
&#13;
    $relpath = NULL;        &#13;
    for ($i = 0; $i &lt; count($tmp); $i++) {&#13;
        if ($tmp[$i] != '') {&#13;
            $relpath .= '../';&#13;
		}&#13;
    }&#13;
&#13;
    if ($relpath != NULL) { &#13;
		$relpath = substr($relpath, 0, -1); &#13;
	} else { &#13;
		$relpath = '.'; &#13;
	}&#13;
	&#13;
    return $relpath;&#13;
}&#13;
[code]&lt;/p&gt;</description>
      <pubDate>Mon, 28 Mar 2011 06:37:43 UTC</pubDate>
      <guid>https://ipv6.snipplr.com/view/2467/get-relative-path-to-root</guid>
    </item>
    <item>
      <title>jHulbert said on 28/Mar/2011</title>
      <link>https://ipv6.snipplr.com/view/2467/get-relative-path-to-root</link>
      <description>&lt;p&gt;function spec_root($root=NULL) {&#13;
	$cur_file = $_SERVER["SCRIPT_NAME"];&#13;
    $cur_file = explode('/', $cur_file);&#13;
    $cur_file = $cur_file[count($cur_file) - 1];&#13;
	$tmp = split($root, $_SERVER['REQUEST_URI']);&#13;
	$tmp = str_replace($cur_file, "", $tmp);&#13;
	$tmp = $tmp[1];&#13;
    $tmp = str_replace('\\', '/', $tmp);&#13;
    $tmp = explode('/', $tmp);&#13;
&#13;
    $relpath = NULL;        &#13;
    for ($i = 0; $i &lt; count($tmp); $i++) {&#13;
        if ($tmp[$i] != '') {&#13;
            $relpath .= '../';&#13;
		}&#13;
    }&#13;
&#13;
    if ($relpath != NULL) { &#13;
		$relpath = substr($relpath, 0, -1); &#13;
	} else { &#13;
		$relpath = '.'; &#13;
	}&#13;
	&#13;
    return $relpath;&#13;
}&lt;/p&gt;</description>
      <pubDate>Mon, 28 Mar 2011 06:38:00 UTC</pubDate>
      <guid>https://ipv6.snipplr.com/view/2467/get-relative-path-to-root</guid>
    </item>
    <item>
      <title>jHulbert said on 28/Mar/2011</title>
      <link>https://ipv6.snipplr.com/view/2467/get-relative-path-to-root</link>
      <description>&lt;p&gt;I don' t understand how you're supposed to post code as comment -- sorry.&#13;
&#13;
My previous comment extends on asherber's example. The function removes the currently executing script name and allows you to specify a root folder.&lt;/p&gt;</description>
      <pubDate>Mon, 28 Mar 2011 06:41:21 UTC</pubDate>
      <guid>https://ipv6.snipplr.com/view/2467/get-relative-path-to-root</guid>
    </item>
  </channel>
</rss>
