note 24760 added to function.dirname
| From: | nsefton_nospam_ at hotmail dot com | Date: | Wed, 28 Aug 2002 00:03:51 +0000 |
| Subject: | note 24760 added to function.dirname | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-35820@lists.php.net to get a copy of this message | ||
Well i had this same problem, includes including files and how to get to overcome the problem of the
first file can see the second, but the second does not know where it is being called from, and thus
cant call the third file, so i came up with this solution...
<?php
function getPath() {
// get position of current file
$path = $GLOBALS["PATH_INFO"];
// save a few nanaseconds, has this been done before,
// has the global variable been set ?
if (!isset($definedPath)) {
// is file in root already ?
if (strrpos($path,"/")!=0) {
// there is probably a quicker way to count
// the "/"s in the path
$path=explode("/",$path);
$s="";
for ($i=0;$i<count($path)-1;$i++) {
if ($path[$i]!="") {
// add the number of "../"s required
$s.="../";
}
}
// declare a global variable so you dont have to
// do this again, and return the variable
global $definedPath;
$definedPath=$s;
return $definedPath;
}
} else {
// just return what it worked out before, function
// only really runs once
return $definedPath;
}
}
include getPath() . "my_include_path/header.inc";
include getPath() . "includes/somethingelse.inc";
//above file is at mydomain.com/includes/somethingelse.inc
include getPath() . "includes/footer.inc";
?>
So basically, this finds the "path back home" and from there it knows it's way
around. I like Fredric's solution ( even tho I dont understand it :P ) but i thought I'd
share what I've done (with room for improvement i'm sure).
Noel
--
http://www.php.net/manual/en/function.dirname.php
http://master.php.net/manage/user-notes.php?action=edit+24760
http://master.php.net/manage/user-notes.php?action=delete+24760
http://master.php.net/manage/user-notes.php?action=reject+24760