2

Does the $_SERVER["DOCUMENT_ROOT"] variable exist on IIS running PHP? Earlier I thought this variable is Apache specific and on IIS, you have to emulate it by string manipulation of SCRIPT_NAME and SCRIPT_FILENAME variables but I now see this variable on my PHP installation on IIS. Is it safe to assume that this variable will always be available on IIS.

4 Answers 4

1

IIS doesn't always set $_SERVER['DOCUMENT_ROOT']

How do you set it in a configuration file, so the rest of your code works like on Apache servers?

Output $_SERVER to see what IS given that you might be able to use:

echo "<br>_SERVER:<br><pre>";
print_r($_SERVER);
echo "</pre><br><br>_ENV:<br><pre>";
print_r($_ENV);
echo "</pre><br><br>";

In this case, the SCRIPT_FILENAME and SCRIPT_NAME are set.

Modify the code below to use what is given to get DOCUMENT_ROOT:

if (!isset($_SERVER['DOCUMENT_ROOT']) || $_SERVER['DOCUMENT_ROOT'] === '') {
    $_SERVER['DOCUMENT_ROOT'] = substr($_SERVER['SCRIPT_FILENAME'], 0, -strlen($_SERVER['SCRIPT_NAME']));
    putenv('DOCUMENT_ROOT='.$_SERVER['DOCUMENT_ROOT']);
}

Now you can use $_SERVER['DOCUMENT_ROOT'] normally:

$docroot = getenv("DOCUMENT_ROOT"); 
include_once "$docroot/folder/yourfile.php"; 
Sign up to request clarification or add additional context in comments.

Comments

0

Is it safe to assume doc root is always available in IIS? No...

$_SERVER['DOCUMENT_ROOT'] is not always available on IIS.. it has to be set in the config file...

If it is configured on your server you 'could' use it... Just make sure your config file doesn't change - otherwise you will break your scripts...

7 Comments

config file = php.ini or something else?
i know in php.ini you can set doc_root (its under the path section) ... But that sets the global doc_root...
Just did a google search: forums.iis.net/p/1154314/1932457.aspx Is an overview of some of the problems with doc_root and multiple sites... Towards the bottom it references a few links to read up on... If you only have one site its not an issue...
2 out of 3 IIS servers display the $_SERVER["DOCUMENT_ROOT"]. 1 out of these 2 show $_ENV["DOCUMENT_ROOT"] as well. So yes, seems like its not always available.
What does it mean that this variable is not always present on IIS? What does it depend on? I experienced similar problems when running some "cron" tasks via Task Scheduler, php-cgi.exe - but when just simply browsing the websites via a regular browser, I found that $_SERVER['DOCUMENT_ROOT'] DOES exist (at least as in many cases as I tried). Another interesting phenomenon: I'm using Xdebug, because it dumps the variables in an easy to follow, colored format, but when hitting F5 multiple times, the Xdebug coloring sometimes doesn't appear to work - kind of like it wasn't loaded at all.Why?
|
0

I solved it by simply referencing my web root, and setting it as one of my own variables.

<?php
echo getcwd();
chdir('/');
echo getcwd();
chdir('/example-web-server');
echo getcwd();
?>

The following code gets the current working directory of PHP, which will be the directory containing the file you're running this on. chdir('/') goes to the root of wherever PHP can operate, in my case C:\. My example web server's web root is at C:\example-web-server, so you can reference it in PHP like this: /example-web-server.

Once you've got the path for PHP, you could set it as a variable and call it. I'll use an example of an include() of C:\example-web-server\testing\index.php:

<?php
$webroot = "/example-web-server";
include("{$webroot}/testing/index.php");
?>

I know this is an old thread, but I can't be the only one needing a solution for this.

Comments

0

This is what I did on top of index.php:

if(!isset($_SERVER["DOCUMENT_ROOT"]))
{
    $_SERVER["DOCUMENT_ROOT"]=getcwd();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.