note 74859 deleted from function.include by danbrown

From: Date: Mon, 18 May 2009 01:00:03 +0000
Subject: note 74859 deleted from function.include by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155463@lists.php.net to get a copy of this message
Note Submitter: cmedina at bluecorestudio dot com ---- I wanted that included files behave like in C/C++ and this was killing me. So I created this function that really helped me (Note: You should add these lines to EVERY included file). Code Lines: <? $FILE_PATH = preg_replace_callback( '/(.*)(\\\?.*?)\s*;\1(\\\?.*?)\s*$/', create_function( '$matches', '$path = str_replace("\\\", "/",(isset($matches[2])? preg_replace( \'/(?:^\/|^\\\\\)?[^\/\\\\\]+(?:\/|\\\\\)?/\', "../",$matches[2]):"./"). (isset($matches[3])?$matches[3]:"")); return !empty($path)? "$path/" : "./";' ) ,realpath("./").";".dirname(__FILE__) ); ?> Usage Example: Files hierarchy for example /www/file.php /include/secondfile.php /include/test/anotherfile <? //file.php //i will not include the code lines in example to avoid repetition but you have to // --- FILE_PATH code lines here --- // include the file relative to the caller position remember to use a relative path from each file to desired file include ($FILE_PATH . "../../include/secondfile.php"); ?> <? //secondfile.php // --- FILE_PATH code lines here --- //note that path used is in reference to secondfile.php's path and not the original caller's(file.php) path include ($FILE_PATH . "test/anotherfile"); //or include ($FILE_PATH . "./test/anotherfile"); ?> <? //anotherfile /* --- some mixed content here --- */ ?> Now you can do recursive includes to files inside already included files using each file's path as reference, like in c/c++!!! I'd tested this just in WINXP (PHP Version 4.4.1), so, I dont know how it behaves in other OS/PHP-Versions. Any additional suggestions or bugs, please let me know.

« previous php.notes (#155463) next »