/ Published in: PHP
Expand |
Embed | Plain Text
Comments
Subscribe to comments
You need to login to post a comment.
frankyfish on 04/13/07
6 people have marked this snippet as a favorite
basicmagic
vali29
hudge
heinz1959
benrasmusen
jonhenshaw
Subscribe to comments
You need to login to post a comment.
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;
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
Thanks for the code, though it doesn't seem to work for me. It always returns "../" whether I'm 1 or 2 directories deep.
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.
Thanks for the feedback
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.
[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);
} [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);
}
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.