Jump to content

Why is DOCUMENT_ROOT different than what I get with getcwd() ?


guttyguppy

Recommended Posts

Here's my code:

 

$curDir=getcwd();
$docRoot=$_SERVER['DOCUMENT_ROOT'];
echo "current dir is:".$curDir;
echo "<br/>";
echo "doc root is:".$docRoot;

 

 

I'm getting :

current dir is:/home/username/public_html

doc root is:/var/www/

 

Why aren't those the same thing? I am having a really hard time understanding server environments, directory paths, so if you can forward any enlightening tutorials/explainations, it would be greatly appreciated. I've checked php.net, but I still don't get it.

Thanks!

Link to comment
Share on other sites

cwd is the current working directory and DOCUMENT_ROOT is:

The document root directory under which the current script is executing, as defined in the server's configuration file.
See: http://www.php.net/manual/en/reserved.variables.server.php

 

The current working directory changes depending on what's going on. If you are calling: www.yoursite.com/index.php the cwd would be something like what you said "/home/username/public_html" if you executed the same script in a sub-directory: www.yoursite.com/subDirectory/index.php the cwd would now be "/home/username/public_html/subDirectory" but DOCUMENT_ROOT would not change because it is what it says above.

 

You can change the current working directory in your scripts too, using functions like chdir() http://au2.php.net/manual/en/function.chdir.php

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.