You are using a browser which is not compatible with CSS (for more information, see Tara's tutorial). Because of this, it is possible that our website may not appear correctly in your browser. We apologise for the inconvenience, and recommend you upgrade your browser to one which is compatible with CSS. For more information, please visit our Browser Upgrade page.

4WebHelp

Scripts: PHP: Find your base directory

This script will output the path to your base directory (aka "Document Root"), which often needs to be used in Perl & PHP scripts.

<?php

###########################################################################
#                                                                         #
# Neither http://www.4webhelp.net/ nor its members accept any             #
# responsibility, either expressed or implied, for any damage caused by   #
# using this script or the misuse of this script.                         #
#                                                                         #
#                                                                         #
#                          INSTRUCTIONS                                   #
#                                                                         #
# 1) Copy this code to an editor such as Notepad and save it with a       #
# .php  extension.                                                        #
# 2) FTP this file to a folder on your site in ASCII mode                 #
# 3) Call up this file in your web browser to see your document root      #
#                                                                         #
###########################################################################

print ($_SERVER['DOCUMENT_ROOT']);

?>

<p>This script courtesy of <a href="http://www.4webhelp.net/">4WebHelp</a>.</p>
Latest comments
max
doesn't work for sub-domains, because it will only find the base of the sub-domain thus it will point to the main domain instead of the REAL base.
Alex
Hi

I have 2 flash applications in my website that allows fileupload. As I wanted to change the upload size limit to 30 or 40 MB, I wrote a
.htaccess file with (only):

php_value upload_max_filesize 35M
php_value post_max_size 50M
php_value memory_limit 48M

I used your PHP file in order to know what's my directory ROOT and I loaded my .htaccess file into there. I didn't had even the chance to try the bigsized upload because, I JUST DON'T HAVE ACCESS to my homepage anymore.

Is there a way to resolve this problem?

Thankx in advance!
Chris
Thank you VERY much guys! I truly thank you! At last a script that helped us!
KingIsulgard
You can get the current directory with this
$currentDirectory = array_pop(explode("/", getcwd()))

Just keep popping until you get to the home directory. For each pop, add a "../" before the relative addressing.

It can be done by a simple loop.
Someone
Should i be the only person to comment that this is not a script... This is simply an example usage?
Sean
$_SERVER['DOCUMENT_ROOT'] is NOT a reliable way to determine the filesystem path to the root of the current website, it only works in apache if you have specificly set "DocumentRoot" in your vhost container. that value comes directly from what you enter into this setting.

In cases where you do not set DocumentRoot in the vhost, it will NOT be availible in $_SERVER['DOCUMENT_ROOT']

the Apache developers also state in many bug reports that DocumentRoot should not be relied upon.
Mike
Wow, thanks a million
Rich
Try it on IIS... it wont work.

This article explains why
http://www.helicron.net/php/
kR105
Helped a lot!!

Thank you!
Ron
thank you for this! Really helped!
Mark
Would it not be better just to use an info.php file for all this stuff, you not only get paths but all Apache info
Paula
My register globals are off, yet "echo getcwd()" works for me.
fwd
echo getcwd();  

That prints the current working dir. and always works...
Arkitekt
PERFECT... exactly what I needed.  Was dealing with a host that had a file structure that I was totally unfamiliar with and the CMS program required the full path to root...

THANKS!
Tim B
omg i cant beleave its that simple, ive been lookin for this exact thing for the past 15 hours and i couldnt find it in my php books or on php.net, cant beleave its as simple as "print ($_SERVER['DOCUMENT_ROOT']);" thank you for this
thessoro
<?php echo $_SERVER['DOCUMENT_ROOT']; ?> will always work from PHP 4.1.0 version. The script above depends on "register_globals" being "On" (in that case you'd better turn it Off)
register_globals is off by default since PHP 4.2.0 due to the potencial risk of variable poisoning.
gabriella
superb! a truly great little script!

Add a new comment

This page is © Copyright 2002-2017, 4WebHelp. It may not be reproduced without 4WebHelp's prior permission.