Server variable $_SERVER["DOCUMENT_ROOT"]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sakthipro
    New Member
    • May 2007
    • 18

    #1

    Server variable $_SERVER["DOCUMENT_ROOT"]

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

    fopen($_SERVER["DOCUMENT_R OOT"]."/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
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Sakthi.

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

    Comment

    • sakthipro
      New Member
      • May 2007
      • 18

      #3
      Heya,
      I didn't find the variable $_SERVER["DOCUMENT_R OOT"] in the phpinfo() in the Windows server. How to enable this variable in Windows Server?

      Sakthi

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Sakthi.

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

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

        Alternatively, if you have a file at the document root, you can use dirname(__FILE_ _).

        Comment

        • sakthipro
          New Member
          • May 2007
          • 18

          #5
          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_TRAN SLATED"] 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

          Comment

          • sakthipro
            New Member
            • May 2007
            • 18

            #6
            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

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, Sakthi.

              Try creating a common include file in your document root:

              [code=php]
              <?php
              if( empty($_SERVER['DOCUMENT_ROOT']) )
              {
              $_SERVER['DOCUMENT_ROOT'] = dirname(__FILE_ _);
              }
              ?>
              [/code]

              Then simply include() this file at the top of any script that needs to use $_SERVER['DOCUMENT_ROOT'].

              Comment

              Working...