note 36658 added to function.include
| From: | srjosh at !nospam! dot maestrakara dot com | Date: | Fri, 17 Oct 2003 22:26:41 +0000 |
| Subject: | note 36658 added to function.include | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-58773@lists.php.net to get a copy of this message | ||
I noticed several solutions regarding different ways of addressing filepaths in includes -
particularly multi-tiered includes. However, none of them worked for me. Most of the ones I found
seem to rely on root-level addressing, which wasn't working for me. My server is set to not
accept variables from external pages, and it was reading the root addressed pages as external.
Anyway, here is what I came up with:
// Send this function a root-formatted address and it will calculate a relative address -
// i.e. $dest="/includes/format_include.php" will be translated to
"../../includes/format_include.php"
function new_address($dest){
//splits current directory into an array to be compared to target directory.
$current = explode ("/", $_SERVER["SCRIPT_NAME"]);
//splits target directory into an array to be compared to current directory.
$dest = explode ("/", $dest);
$relative_toggle=true;
for ($x=0;$x<count($dest);$x++){
debug($x);
if ($relative_toggle==true){
if ($dest[$x]==$current[$x]){
$toURL = $toURL . "../";
} else {
$relative_toggle = false;
}
} else {
if ($x == count($dest)-1) {
$toURL = $toURL . $dest[$x];
} else {
$toURL = $toURL . $dest[$x] . "/";
}
}
}
return ($toURL);
}
It works by splitting both current address (as reported by $_SERVER["SCRIPT_NAME"]) and
the target address as fed to the function into arrays based on the slashmarks, then running a
comparison.
Each array value correspondes to a directory, of course. It seemed counter-intuitive to me at
first, but if the target and current arrays MATCH, then the relative URL you are building needs a
"../" for that level. If they DON'T match, then you are at the point where the
target directory structure takes over. This is where the $relative_toggle comes into play.
Finally, do note that $_SERVER["SCRIPT_NAME"] doesn't report query strings, so if
that is important to you, you might need to change that part of the code.
----
Manual Page -- http://www.php.net/manual/en/function.include.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+36658
Delete -- http://master.php.net/manage/user-notes.php?action=delete+36658&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+36658&report=yes
Search -- http://master.php.net/manage/user-notes.php