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_pag e( $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_direct ory();
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?
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_pag e( $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_direct ory();
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?
Comment