Page 1 of 1

Getting The Web App's Root Directory

Posted: Fri Feb 16, 2007 3:12 am
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.

Posted: Fri Feb 16, 2007 3:16 am
by shwanky

Code: Select all

<?php

echo $_SERVER['DOCUMENT_ROOT'];

?>

Posted: Fri Feb 16, 2007 3:19 am
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.

Posted: Fri Feb 16, 2007 3:31 am
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.

Posted: Fri Feb 16, 2007 3:54 am
by Jenk
another solution:

Code: Select all

echo realpath('.');

Posted: Fri Feb 16, 2007 5:30 am
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...

Posted: Fri Feb 16, 2007 6:00 am
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__);
}

Posted: Fri Feb 16, 2007 8:21 am
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.