getting absolute directory path?

getting absolute directory path?

am 24.04.2008 17:32:39 von Law Poop

Hello all -

I have a two part question.

First of all, I have a website under /home/user/www/. The index.php
and all the other website pages are under /home/user/www/. For
functions that are used in multiple files, I have php files under /
home/user/www/functions/. These files simply have

So, in index.php and other files, I have
include("./functions/function.create_html_page.inc");
include("./functions/function.create_pdf.inc");
.....
?>

And then function files, such as function.create_html_page.inc
function create_html_page( $arg1, $arg2 ) {
...
}
?>

So, my php pages have relative references to the function files, i.e.
"./functions/".

This works okay, but when I want to include a function in a function
file, I have to change the relative path. So, instead of
include("./functions/function.create_pdf.inc");
I should have
include("./function.create_pdf.inc");

Ideally I would like to figure out the absolute path name, so that I
don't have to change the relative path names depending on whether the
file is in the root directory or the functions directory, or any other
directory for that matter.

In other words, I could do
$path = "/home/user/www/";
include( $path . "functions/function.create_html_page.inc");
include( $path . "functions/function.create_pdf.inc");
....
?>

And then the include statements are identical, no matter what
directory they're in.

But, I would like the solution to be more portable. In change I change
hosts, or install the website on another server, I would like the path
name not to be hard-coded, so I wouldn't have to change anything.
Something like

$path = get_base_directory();
include( $path . "functions/function.create_html_page.inc");
include( $path . "functions/function.create_pdf.inc");
....
?>

My website is designed so that pages in subdirectories are never
served; the user is always navigating in the root directory. So, even
if a function in the functions subdirectory are called, the original
executing script was in the root.

I looked at functions like basename, but they expect the directory as
an argument.

I looked at $_SERVER and $_ENV, and they have 'OLDPWD'
[OLDPWD] => /home/user/www
[PWD] => /home/user/www/functions

OLDPWD looks like it would work, but I couldn't find it documented
anywhere, so I don't know if it would be useable as a portable
solution. If it's not documented, I probably can't rely on it being on
most systems, right?

Anywho, my thought now is to go through the backtrace array to figure
out the originating script, and thus the base directory. Is there an
easier way to do this?

Re: getting absolute directory path?

am 24.04.2008 17:52:00 von luiheidsgoeroe

lawpoop@gmail.com wrote:
> Hello all -
>
> I have a two part question.
>
> First of all, I have a website under /home/user/www/. The index.php
> and all the other website pages are under /home/user/www/. For
> functions that are used in multiple files, I have php files under /
> home/user/www/functions/. These files simply have
>
> So, in index.php and other files, I have
> > include("./functions/function.create_html_page.inc");
> include("./functions/function.create_pdf.inc");
> .....
> ?>
>
> And then function files, such as function.create_html_page.inc
> > function create_html_page( $arg1, $arg2 ) {
> ...
> }
> ?>
>
> So, my php pages have relative references to the function files, i.e.
> "./functions/".
>
> This works okay, but when I want to include a function in a function
> file, I have to change the relative path. So, instead of
> include("./functions/function.create_pdf.inc");
> I should have
> include("./function.create_pdf.inc");
>
> Ideally I would like to figure out the absolute path name, so that I
> don't have to change the relative path names depending on whether the
> file is in the root directory or the functions directory, or any other
> directory for that matter.
>
> In other words, I could do
> > $path = "/home/user/www/";
> include( $path . "functions/function.create_html_page.inc");
> include( $path . "functions/function.create_pdf.inc");
> ....
> ?>
>
> And then the include statements are identical, no matter what
> directory they're in.
>
> But, I would like the solution to be more portable. In change I change
> hosts, or install the website on another server, I would like the path
> name not to be hard-coded, so I wouldn't have to change anything.
> Something like
>
> > $path = get_base_directory();
> include( $path . "functions/function.create_html_page.inc");
> include( $path . "functions/function.create_pdf.inc");
> ....
> ?>
>
> My website is designed so that pages in subdirectories are never
> served; the user is always navigating in the root directory. So, even
> if a function in the functions subdirectory are called, the original
> executing script was in the root.
>
> I looked at functions like basename, but they expect the directory as
> an argument.
>
> I looked at $_SERVER and $_ENV, and they have 'OLDPWD'
> [OLDPWD] => /home/user/www
> [PWD] => /home/user/www/functions
>
> OLDPWD looks like it would work, but I couldn't find it documented
> anywhere, so I don't know if it would be useable as a portable
> solution. If it's not documented, I probably can't rely on it being on
> most systems, right?
>
> Anywho, my thought now is to go through the backtrace array to figure
> out the originating script, and thus the base directory. Is there an
> easier way to do this?

Either use $_SERVER['DOCUMENT_ROOT'], or use the include_path setting,
and don't provide the directory at all on an include.

--
Rik Wasmus

Re: getting absolute directory path?

am 24.04.2008 18:10:30 von Guillaume

lawpoop@gmail.com a écrit :
> This works okay, but when I want to include a function in a function
> file, I have to change the relative path. So, instead of
> include("./functions/function.create_pdf.inc");
> I should have
> include("./function.create_pdf.inc");

In a function file:
include (dirname(__FILE__) . './function.create_pdf.inc");

I would also recommand to use .inc.php so your php code cannot be
displayed in a browser, only executed.

Regards,
--
Guillaume