Getting The Web App's Root Directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jorge
Forum Newbie
Posts: 14
Joined: Tue Mar 21, 2006 8:36 pm

Getting The Web App's Root Directory

Post by Jorge »

Hi,

I can get the current directory with getcwd() but I'd like to know if there's a similar function to get the web application's root directory?
For instance, a getcwd() call would return the following:
/home/jorge/release/project/webapp/controller/reporter
That's when getcwd() is executed in a file within that "reporter" folder. But I'm more interested in retrieving the following:
/home/jorge/release/project/webapp
The above is is my web application's home directory.
I really don't want to hard code any path strings here. Any hints? Am I missing a function to give me what I want?

Thanks.
shwanky
Forum Commoner
Posts: 45
Joined: Thu Feb 15, 2007 1:21 am

Post by shwanky »

Code: Select all

<?php

echo $_SERVER['DOCUMENT_ROOT'];

?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I'm not 100% what you're asking but if the above doesn't answer your question, perhaps you're lookig for this:

Code: Select all

echo dirname(__FILE__);
That will give you the directory that the current file is in, even if it was included into another file.
Jorge
Forum Newbie
Posts: 14
Joined: Tue Mar 21, 2006 8:36 pm

Post by Jorge »

shwanky's solution isn't right for me because it returns /var/www. I guess it doesn't take into account aliases.
I'll try yours soon enough. Right now I'm just calling getcwd() from my index.php (which is located under the app's root directory) and storing the result in session so I can reuse it whenever I want to.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

another solution:

Code: Select all

echo realpath('.');
Jorge
Forum Newbie
Posts: 14
Joined: Tue Mar 21, 2006 8:36 pm

Post by Jorge »

echo realpath('.') gives me the same thing as getcwd(); the current work directory and not the root directory. I managed to find another work around using dirname(__FILE__). But it's basically the same approach I used mentioned in my previous post...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I wouldn't store the value as session data but include a config file when needed.
e.g.

Code: Select all

require '../../config.php';
and this config file has something like

Code: Select all

<?php
$project_root_dir = dirname(__FILE__);
// or
function project_directory() {
	return dirname(__FILE__);
}
Jorge
Forum Newbie
Posts: 14
Joined: Tue Mar 21, 2006 8:36 pm

Post by Jorge »

Yup, that's the solution I've swotched to. Not exactly what I was looking for at first but it will do. I can't waste anymore time on this issue :)
Thank you all.
Post Reply