By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,558 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,558 IT Pros & Developers. It's quick & easy.

Server variable $_SERVER["DOCUMENT_ROOT"]

P: 18
Hi,
I am using the Linux Server and server variable $_SERVER["DOCUMENT_ROOT"] to get the root folder to open a text file located in the folder for ex.

fopen($_SERVER["DOCUMENT_ROOT"]."/folder/file.txt","r");
......


I want to use the same scripts to work with Windows Server, Is there any way to get the root folder of the domain installation in Windows?
like it should return c:\www


Could any one suggest me?

Thanks in advance

Sakthi
Dec 25 '07 #1
Share this Question
Share on Google+
6 Replies


pbmods
Expert 5K+
P: 5,821
Heya, Sakthi.

$_SERVER['DOCUMENT_ROOT'] evaluates to the path to your server's document root regardless of operating system....
Dec 25 '07 #2

P: 18
Heya,
I didn't find the variable $_SERVER["DOCUMENT_ROOT"] in the phpinfo() in the Windows server. How to enable this variable in Windows Server?

Sakthi
Dec 26 '07 #3

pbmods
Expert 5K+
P: 5,821
Heya, Sakthi.

... except for Windows, it seems, which likes to make life difficult.

Do you have $_SERVER['ORIG_PATH_TRANSLATED'] by any chance?

Alternatively, if you have a file at the document root, you can use dirname(__FILE__).
Dec 27 '07 #4

P: 18
Hi,
Its working in Windows, but is not the situation,
I want a single variable that returns the document root in both servers,
here the _SERVER["ORIG_PATH_TRANSLATED"] variable is not available in my Linux Server.

For ex,
1. In Linux Server it should refer /www/project
2. Where as in Windows Server it should return D:\www\project

where project is the root for the particular project

Suggest me

Sakthi
Dec 27 '07 #5

P: 18
Heya,
Actually the problem with dirname(__FILE__) is,
When I call this from the root, ie. from /www/projects it solves the situation.

The problem is, when I call from the sub directory inside the root ie.

From /www/projects/aaa, I want to refer the path /www/projects/bbb, here the function returns /www/projects/aaa as result, so the final result will be,

dirname(__FILE__)."/bbb" = /www/projects/aaa/bbb which is wrong

Suggest me

Sakthi
Dec 29 '07 #6

pbmods
Expert 5K+
P: 5,821
Heya, Sakthi.

Try creating a common include file in your document root:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     if( empty($_SERVER['DOCUMENT_ROOT']) )
  3.     {
  4.         $_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);
  5.     }
  6. ?>
  7.  
Then simply include() this file at the top of any script that needs to use $_SERVER['DOCUMENT_ROOT'].
Dec 30 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.