note 24760 deleted from function.dirname by danbrown

From: Date: Sun, 20 Jul 2008 19:15:31 +0000
Subject: note 24760 deleted from function.dirname by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142283@lists.php.net to get a copy of this message
Note Submitter: nsefton_nospam_ at hotmail dot com ---- 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

« previous php.notes (#142283) next »