I think the correct way is to do:
if(file_exists(stream_resolve_include_path($filepath))){
include $filepath;
}
This is because the documentation says that stream_resolve_include_path
resolves the "filename against the include path according to the same rules as fopen()/include."
Some people suggested using is_file
or is_readable
but that´s not for the general use case because in the general use, if the file is blocked or unavailable for some reason after file_exists returns TRUE, that´s something you need to notice with a very ugly error message right on the final user´s face or otherwise you are open to unexpected and inexplicable behavior later with possible loss of data and things like that.
file_exists()
: checks whether a file or directory exists.is_file()
would be a better fit in this case.is_file()
is better thanfile_exists()
but isn't it even better to useis_readable()
?is_readable()
: "Returns TRUE if the file or directory specified by filename exists and is readable, FALSE otherwise".