note 76693 added to function.include

From: Date: Thu, 26 Jul 2007 15:07:11 +0000
Subject: note 76693 added to function.include
Groups: php.notes 
Request: Send a blank email to php-notes+get-128942@lists.php.net to get a copy of this message
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 ---- Server IP: 69.147.83.197 Probable Submitter: 194.102.62.253 ---- Manual Page -- http://www.php.net/manual/en/function.include.php Edit -- https://master.php.net/note/edit/76693 Del: integrated -- https://master.php.net/note/delete/76693/integrated Del: useless -- https://master.php.net/note/delete/76693/useless Del: bad code -- https://master.php.net/note/delete/76693/bad+code Del: spam -- https://master.php.net/note/delete/76693/spam Del: non-english -- https://master.php.net/note/delete/76693/non-english Del: in docs -- https://master.php.net/note/delete/76693/in+docs Del: other reasons-- https://master.php.net/note/delete/76693 Reject -- https://master.php.net/note/reject/76693 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128942) next »