Posted By

frankyfish on 04/13/07


Tagged


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

basicmagic
vali29
hudge
heinz1959
benrasmusen
jonhenshaw


Get relative path to root


 / Published in: PHP
 

  1. $relpath = "";
  2. $tempvarrelpathdir = explode("/",dirname($SERVER['PHPSELF'])); for($i=count($tempvarrelpathdir); $i>0; $i--) if($tempvar_relpathdir[$i] != '') $relpath .= "../";

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: phatrick on May 21, 2008

Think what you meant was $i>=0; which works not $i>0;

$path = "";

for($i=count(arraypop(explode("/",dirname($SERVER['PHP_SELF'])))); $i>=0; $i--) $path .= "../";

echo $path;

Posted By: skcsknathan001 on June 20, 2008

Thank you very much.

I was not able to get DOCUMENT_ROOT or any other server variables. All was returning empty.

I used this and it's working. I modified

$path = ""; for($i=count(explode("/",dirname($SERVER['PHPSELF']))); $i>1; $i--) $path .= "../"; echo $path;

this works from root folder itself and any level of subfolders

Thanks again

Posted By: relaxasaurus on December 4, 2008

Thanks for the code, though it doesn't seem to work for me. It always returns "../" whether I'm 1 or 2 directories deep.

Posted By: blackhole12 on June 30, 2009

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

$relpath = ""; $tempvarrelpathdir = explode("/",dirname($SERVER['PHPSELF'])); for($i=count($tempvarrelpathdir); $i>0; $i--) if($tempvar_relpathdir[$i] != '') $relpath .= "../";

Hopefully since this is the first result that comes up on google, this will save the pain of others in the same situation.

Posted By: frankyfish on March 17, 2010

Thanks for the feedback

Posted By: asherber on June 30, 2010

The code from blackhole12 doesn't work on Windows. It also always gives a trailing slash -- except if you're already at the root, in which case it returns ''. Since most of the time you probably want to write something like "$pathtoroot/foobar", this is not ideal. The following function omits the trailing slash if present and returns '.' for the root, so that you can always safely add '/foobar' to get where you want to be.

function path_to_root() {
    $tmp = dirname($_SERVER[PHP_SELF]);
    $tmp = str_replace('\\', '/', $tmp);
    $tmp = explode('/', $tmp);

    $relpath = NULL;        
    for ($i = 0; $i < count($tmp); $i++) {
        if ($tmp[$i] != '') 
            $relpath .= '../';
    }

    if ($relpath != NULL)
        // remove trailing slash
        $relpath = substr($relpath, 0, -1);
    else
        // return a dot so we can add a slash later on
        $relpath = '.';

    return $relpath;
}
Posted By: jHulbert on March 28, 2011

[code:] function specroot($root=NULL) { $curfile = $SERVER["SCRIPTNAME"]; $curfile = explode('/', $curfile); $curfile = $curfile[count($curfile) - 1]; $tmp = split($root, $SERVER['REQUESTURI']); $tmp = strreplace($curfile, "", $tmp); $tmp = $tmp[1]; $tmp = strreplace('\', '/', $tmp); $tmp = explode('/', $tmp);

$relpath = NULL;        
for ($i = 0; $i < count($tmp); $i++) {
    if ($tmp[$i] != '') {
        $relpath .= '../';
    }
}

if ($relpath != NULL) { 
    $relpath = substr($relpath, 0, -1); 
} else { 
    $relpath = '.'; 
}

return $relpath;

} [code]

Posted By: jHulbert on March 28, 2011

function specroot($root=NULL) { $curfile = $SERVER["SCRIPTNAME"]; $curfile = explode('/', $curfile); $curfile = $curfile[count($curfile) - 1]; $tmp = split($root, $SERVER['REQUESTURI']); $tmp = strreplace($curfile, "", $tmp); $tmp = $tmp[1]; $tmp = strreplace('\', '/', $tmp); $tmp = explode('/', $tmp);

$relpath = NULL;        
for ($i = 0; $i < count($tmp); $i++) {
    if ($tmp[$i] != '') {
        $relpath .= '../';
    }
}

if ($relpath != NULL) { 
    $relpath = substr($relpath, 0, -1); 
} else { 
    $relpath = '.'; 
}

return $relpath;

}

Posted By: jHulbert on March 28, 2011

I don' t understand how you're supposed to post code as comment -- sorry.

My previous comment extends on asherber's example. The function removes the currently executing script name and allows you to specify a root folder.

You need to login to post a comment.