note 74859 added to function.include
| From: | cmedinaatbluecorestudiodotcom at osu1 dot php dot net | Date: | Tue, 01 May 2007 05:03:56 +0000 |
| Subject: | note 74859 added to function.include | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126005@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 64.71.164.2
Probable Submitter: 64.237.231.136
----
Manual Page -- http://www.php.net/manual/en/function.include.php
Edit -- https://master.php.net/note/edit/74859
Del: integrated -- https://master.php.net/note/delete/74859/integrated
Del: useless -- https://master.php.net/note/delete/74859/useless
Del: bad code -- https://master.php.net/note/delete/74859/bad+code
Del: spam -- https://master.php.net/note/delete/74859/spam
Del: non-english -- https://master.php.net/note/delete/74859/non-english
Del: in docs -- https://master.php.net/note/delete/74859/in+docs
Del: other reasons-- https://master.php.net/note/delete/74859
Reject -- https://master.php.net/note/reject/74859
Search -- https://master.php.net/manage/user-notes.php