Check if an include (or require) exists

PhpIncludeExistsRequire

Php Problem Overview


How do you check if an include / require_once exists before you call it, I tried putting it in an error block, but PHP didn't like that.

I think file_exists() would work with some effort, however that would require the whole file path, and a relative include could not be passed into it easily.

Are there any other ways?

Php Solutions


Solution 1 - Php

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 @.

Solution 2 - Php

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

Solution 3 - Php

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*/ }

Solution 4 - Php

file_exists would work with checking if the required file exists when it is relative to the current working directory as it works fine with relative paths. However, if the include file was elsewhere on PATH, you would have to check several paths.

function include_exists ($fileName){
    if (realpath($fileName) == $fileName) {
        return is_file($fileName);
    }
    if ( is_file($fileName) ){
        return true;
    }

    $paths = explode(PS, get_include_path());
    foreach ($paths as $path) {
        $rp = substr($path, -1) == DS ? $path.$fileName : $path.DS.$fileName;
        if ( is_file($rp) ) {
            return true;
        }
    }
    return false;
}

Solution 5 - Php

file_exists() works with relative paths, it'll also check if directories exist. Use is_file() instead:

if (is_file('./path/to/your/file.php'))
{
    require_once('./path/to/your/file.php');
}

Solution 6 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMintDepartureView Question on Stackoverflow
Solution 1 - PhpJohannes GorsetView Answer on Stackoverflow
Solution 2 - PhpStephane JAISView Answer on Stackoverflow
Solution 3 - PhpKhawarView Answer on Stackoverflow
Solution 4 - PhpYacobyView Answer on Stackoverflow
Solution 5 - PhpAlix AxelView Answer on Stackoverflow
Solution 6 - PhpjacmknoView Answer on Stackoverflow