Timothy Madden <ba****@rmv.spa m.home.ro> wrote or quoted:
I'm trying to include some files in an included file. Think of some scripts
like this
/index.php
/scripts/logging.php
/scripts/config.php
/scripts/db_access.php
index.php includes 'scripts/logging.php' and logging.php includes
'config.php' ?i 'db_access.php' .
My problem is that from within logging.php the php will try to include the
file '/config.php' and not '/scripts/config.php' because logging.php is
included in '/index.php' in the first place.
I would use absolute paths in the included file names, built with
$_SERVER['DOCUMENT_ROOT'], but I don't really know where my pages are on
the web server. I just know they are somewhere under DocumentRoot.
How can I write pages that can translate local paths to virtual paths and
that can be moved from one place to another in the virtual directory
hierarhy and still function corectly ?
How can I write pages that know about each other's URI's and that do not
care where they are placed on the webserver with respect to DocumentRoot ?
You /could/ use this code:
<?php
function include_once_re lative($path) {
$from = realpath(dirnam e($_SERVER["SCRIPT_FILENAM E"]));
$to = realpath(dirnam e(__FILE__)."/".$path);
include_once(re lative_path($to ,$from));
}
function relative_path ($targetfile, $basedir = '.') {
$basedir = realpath ($basedir);
$targetfile = realpath ($targetfile);
// on windows, check that both paths are on the same drive
if (substr ($basedir, 0, 1) != substr ($targetfile, 0, 1)) {
return false;
}
// split each path into its directories
$base_parts = split ('\/', str_replace ('\\', '/', $basedir));
$target_parts = split ('\/', str_replace ('\\', '/', $targetfile));
// ensure that there are no empty elements at the end (c:\ would cause it)
for ($i = count($base_par ts) - 1; $i >= 0; $i--) {
if ($base_parts[$i] == '') {
unset ($base_parts[$i]);
} else {
break;
}
}
for ($i = count($target_p arts) - 1; $i >= 0; $i--) {
if ($target_parts[$i] == '') {
unset ($target_parts[$i]);
} else {
break;
}
}
// get rid of the common directories at the beginning of the paths
$common_count = 0;
for ($i = 0; $i < count($base_par ts); $i++) {
if ($target_parts[$i] == $base_parts[$i]) {
$common_count++ ;
} else {
break;
}
}
for ($i = 0; $i < $common_count; $i++) {
unset ($base_parts[$i]);
unset ($target_parts[$i]);
}
// build the resulting string
$cnt = count($base_par ts) - 1;
if ($cnt < 1) {
$cnt = 0;
}
return str_repeat ('../', $cnt).implode('/', $target_parts);
}
?>
Then calling "include_once_r elative" with relative paths would include
paths relative to the library code, not relative to the document.
--
__________
|im |yler
http://timtyler.org/ ti*@tt1lock.org Remove lock to reply.