Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

nrao wwwxxx, xxxxxdup, procmail change subject header, wwwXxx not20, Wwwxxx.doks sas, linux raid resync after reboot, bind-address mysql multiple, sanibleone xxxx, ftp://192.168.100.100/, www.xxxcon

Links

XODOX
Impressum

#1: getting absolute directory path?

Posted on 2008-04-24 17:32:39 by 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
<?php
include("./functions/function.create_html_page.inc");
include("./functions/function.create_pdf.inc");
.....
?>

And then function files, such as function.create_html_page.inc
<?php
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
<?php
$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?

Report this message

#2: Re: getting absolute directory path?

Posted on 2008-04-24 17:52:00 by 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
> <?php
> include("./functions/function.create_html_page.inc");
> include("./functions/function.create_pdf.inc");
> .....
> ?>
>
> And then function files, such as function.create_html_page.inc
> <?php
> 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
> <?php
> $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

Report this message

#3: Re: getting absolute directory path?

Posted on 2008-04-24 18:10:30 by 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

Report this message