Forum Moderators: coopster

Message Too Old, No Replies

finding full path to root folder

         

lindajames

1:03 pm on Sep 27, 2003 (gmt 0)

10+ Year Member



Hello,

can anyone tell me how i can find the full path to my root folder using PHP? in asp its like this: <%= Server.MapPath("\")%>

Cheers

jamie

1:08 pm on Sep 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi linda

$_SERVER['DOCUMENT_ROOT'] or $_ENV['DOCUMENT_ROOT']

lindajames

9:19 pm on Sep 27, 2003 (gmt 0)

10+ Year Member



i've tried both of them but it doesnt work. i get a blank page.

any ideas?

jatar_k

3:19 am on Sep 28, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



how exactly are you trying to use it?

dreamcatcher

7:48 am on Sep 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$server = $_SERVER['DOCUMENT_ROOT'];

echo $server;

:)

lindajames

9:35 am on Sep 28, 2003 (gmt 0)

10+ Year Member



dreamcatcher, that worked.

but... i just realised that it gives you the path to the actual script. i just want it to display the path upto the root folder

MonkeeSage

9:40 am on Sep 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All of the CGI environment variables work in PHP, AFAIK. A list is here:

[cgi101.com...]

I think you are wanting the PATH variable, so using dreamcatcher's example...

$server = $_SERVER['PATH'];
echo $server;

Jordan

dreamcatcher

10:09 am on Sep 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can also try:

$server = $_SERVER['SITE_HTMLROOT'];

echo $server;

:)

lindajames

10:42 am on Sep 28, 2003 (gmt 0)

10+ Year Member



i tried both of that, and i get a blank page. and when i change it back to DOCUMENT_ROOT then it works again.

any ideas?

MonkeeSage

2:23 pm on Sep 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try it with $_ENV[] instead of $_SERVER[] and see what you get. If it still doesn't work, mabye your host disabled those environment variables for security reasons or something.

Jordan

MonkeeSage

3:14 pm on Sep 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can also do something like this...I think it will work, but I'm a PHP newbie, so I might have goofed somewhere...

<?php 

$svr = $_SERVER['SCRIPT_NAME'];

function getRoot($root, $svr) {
if ($root!= $svr) {
$root = $svr;
$svr = preg_replace('/(\/.+\/?)/', "$2", $svr);
if ($root!= $svr) {
getRoot($root, $svr);
}
else {
echo $root . '/';
}
}
}

getRoot("", $svr);

?>

You can also try it with...

$svr = $_SERVER['SCRIPT_FILENAME'];

or

$svr = $_SERVER['REQUEST_URI'];

Mabye one of those will work...

Jordan

lindajames

3:54 pm on Sep 28, 2003 (gmt 0)

10+ Year Member



$_SERVER['DOCUMENT_ROOT'] works, isnt there anyway to actually use that but get the php code to get rid of the final part of the path which has the actual file name. for example at the moment when i use the above it gives me the following:

f:\users\l\lindajames\user\htdocs\index.php

can i not get the script to not display everything after the htdocs?

jatar_k

4:58 pm on Sep 28, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



everytime I have ever used it it doesn't return any script name along with the document root. I wonder if it is a windows peculiarity?

$_SERVER['DOCUMENT_ROOT']

that's right and will return the value of the DocumentRoot for the given site from apache.
[httpd.apache.org...]

in this particular case I am thinking you need too look at the apache conf and see what that says.

MonkeeSage

5:15 pm on Sep 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jatar is correct, I've never had DOCUMENT_ROOT returning a filename and I've used it a bunch in Perl scripts, and since it is a CGI variable it is the same value returned to Perl as to PHP...so that's really odd, but even so, to answer your question...

$_SERVER['DOCUMENT_ROOT'] works, isnt there anyway to actually use that but get the php code to get rid of the final part of the path which has the actual file name

...yes, easiest is probably preg_replace(), something like...

$server = preg_replace('/(.+\/).+\..+/', "$1", $_SERVER['DOCUMENT_ROOT']);
echo $server;

Jordan

daisho

1:00 am on Sep 29, 2003 (gmt 0)

10+ Year Member



Has to be a windows or apache.conf thing (though I have no idea how you'd change that in apache.conf. Document Root is Document Root)

I use $_SERVER['DOCUMENT_ROOT'] *all* the time and it gets me just what I want. The directory for the root of the virtualserver that I'm on.

By any change are you running IIS rather than apache on windows? As I remember IIS does not set a DOCUMENT_ROOT this is an apache thing. I remember it giving me some heart ache when I had to do work work on an IIS server.

daisho.