0

Is there a $_SERVER['DOCUMENT_ROOT'] equivalent which would work on both Apache and IIS without having to change configuration files? Or maybe some workarounds?

5
  • What is the issue that you are facing, can you clarify? Commented Feb 5, 2012 at 18:48
  • The problem is that $_SERVER['DOCUMENT_ROOT'] does not work on IIS as I know and it's not a good idea to use it. Also I am searching for a solution which would work on lower versions of PHP as well (lower than PHP 5). Commented Feb 5, 2012 at 18:56
  • Maybe you could use the PHP function getcwd(); (PHP4/PHP5) and some logic to get what you need? Commented Feb 5, 2012 at 19:00
  • On some Unix variants, getcwd() will return FALSE if any one of the parent directories does not have the readable or search mode set, even if the current directory does. Commented Feb 5, 2012 at 19:07
  • I've worked with dozens of UNIX servers, some shared some independent, never has getcwd failed. You should be more focused on creating a viable solution instead of catering to exceptions that you'll likely never encounter. Commented Feb 5, 2012 at 19:26

1 Answer 1

1

For IIS

if(empty($_SERVER['DOCUMENT_ROOT']) && !empty($_SERVER['SCRIPT_FILENAME'])) { 
  $_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/', substr($_SERVER['SCRIPT_FILENAME'], 0, 0 - strlen($_SERVER['PHP_SELF'])));
} 
if(empty($_SERVER['DOCUMENT_ROOT']) && !empty($_SERVER['PATH_TRANSLATED'])) { 
  $_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/', substr(str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED']), 0, 0 - strlen($_SERVER['PHP_SELF'])));
}
Sign up to request clarification or add additional context in comments.

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.