Check if an include (or require) exists Check if an include (or require) exists php php

Check if an include (or require) exists


I believe file_exists does work with relative paths, though you could also try something along these lines...

if(!@include("script.php")) throw new Exception("Failed to include 'script.php'");

... needless to say, you may substitute the exception for any error handling method of your choosing. The idea here is that the if-statement verifies whether the file could be included, and any error messages normally outputted by include is supressed by prefixing it with @.


You can also check for any variables, functions or classes defined in the include file and see if the include worked.

if (isset($variable)) { /*code*/ }

OR

if (function_exists('function_name')) { /*code*/ }

OR

if (class_exists('class_name')) { /*code*/ }


Check out the stream_resolve_include_path function,it searches with the same rules as include().

http://php.net/manual/en/function.stream-resolve-include-path.php