note 76693 deleted from function.include by peehaa
| From: | peehaa@php.net | Date: | Fri, 09 Dec 2016 22:46:12 +0000 |
| Subject: | note 76693 deleted from function.include by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208381@lists.php.net to get a copy of this message | ||
Note Submitter: alex carstea
----
Since include() caused me many problems when i was trying to test my code, I wrote a small function.
It receives as parameter the path to the file to include relative to the current file. The format
similar to :
"../../path/FileName.php"
The function returns the absolute path to the file to be included. This path can be used as argument
to include() and resolves the problem of nested inclusions.
<?php
function getFilePath($relativePath){
$absPath=dirname($_SERVER['SCRIPT_FILENAME']);
$relativeArray=explode("/",$relativePath);
$absArray=explode("/",$absPath);
$upTokens=0;
//count the number of ".." tokens that precede the path
while(( $upTokens<count($relativeArray)) and ($relativeArray[$upTokens]=="..")) {
$upTokens++;
}
// create the absolute path
$filePath=$absArray[0];
for ($i=1; $i< (count($absArray)-$upTokens);$i++) {
$filePath.="/".$absArray[$i];
}
for ($i=$upTokens; $i< count($relativeArray);$i++){
$filePath.="/".$relativeArray[$i];
}
return $filePath;
}
?>
Hope you will find it usefull....
Alex