<?php
// From: http://cn2.php.net/manual/en/function.require-once.php
?>
<?php
function & rel($r, &$f) {return file_exists( ( $f = ( dirname($r).'/'.$f ) ) );}
function & relf($r, $f) {return rel($r,$f) ? file_get_contents($f) : null;}
function & reli($r, $f) {return rel($r,$f) ? include($f) : null;}
function & relr($r, $f) {return rel($r,$f) ? require($f) : null;}
function & relio($r, $f) {return rel($r,$f) ? include_once($f) : null;}
function & relro($r, $f) {return rel($r,$f) ? require_once($f) : null;}
?>
I found it useful to have a function that can load a file relative to the calling script and return null if the file did not exist, without raising errors.
<?php
/*
Load file contents or return blank if it's not there.
Relative to the file calling the function.
*/
echo relf(__FILE__, 'some.file');
?>
It was easy to modify and just as useful for require/include.
<?php
/*
Require the file once.
It's like suppressing error messages with @ but only when the file does not exist.
Still shows compile errors/warning, unless you use @relro().
Relative to the file calling the function.
*/
relro(__FILE__, 'stats.php');
?>
<?php // /var/www/app/system/include.inc.php
function require_once_wildcard($wildcard, $__FILE__) {
preg_match("/^(.+)\/[^\/]+$/", $__FILE__, $matches);
$ls = `ls $matches[1]/$wildcard`;
$ls = explode("\n", $ls);
array_pop($ls); // remove empty line ls always prints
foreach ($ls as $inc) {
require_once($inc);
}
}
?>
The $__FILE__ variable should be filled with the special PHP construct __FILE__:
<?php // /var/www/app/classes.inc.php
require_once('system/include.inc.php');
require_once_wildcard("classes/*.inc.php", __FILE__);
?>