Searching for files in the include_path
Posted on April 5th, 2004 in Code Repository | 10 Comments »
Sometimes, especially when doing lazy-loading (or very lazy loading…), it’s handly to know whether a file exists anywhere along your include_path.
/**
* Check if a file exists in the include path
*
* @version 1.2.1
* @author Aidan Lister <aidan@php.net>
* @link http://aidanlister.com/2004/04/searching-for-files-in-the-include_path/
* @param string $file Name of the file to look for
* @return mixed The full path if file exists, FALSE if it does not
*/
function file_exists_incpath ($file)
{
$paths = explode(PATH_SEPARATOR, get_include_path());
foreach ($paths as $path) {
// Formulate the absolute path
$fullpath = $path . DIRECTORY_SEPARATOR . $file;
// Check it
if (file_exists($fullpath)) {
return $fullpath;
}
}
return false;
}
10 Responses
nice code, very usefull
Thanks for this function,
Very useful function – although I’ve one suggestion.
Instead of returning true – return $fullpath. This will evaluate to true – but also give the caller something to use.
Regards,
Dasher
[Editor's Note: Yes, good point. I've made this change.]
Very nice, but only works with php >= 4.3.0, because of the get_include_path() function… Use ini_get(‘include_path’) instead
Thx for usefull code!
didn’t use the script but used the DIRECTORY_SEPARATOR, v.useful as i was being silly when building on a windows box then running on a Linix box. file_exists() was giving me a duff answer coz of the \ / thing. Thx
Excellent code, thanks!
this code works super!
thx @ u *gg*
Useful function – thanks very much!
Exactly what I was lookin for, thanks a lot.
IMHO cleaner and more powerful solution than silent includes :
if (file_exists_incpath($file)) {
include $file;
}
instead of
@include $file;