50

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?

Alix Axel
  • 141,486
  • 84
  • 375
  • 483
MintDeparture
  • 5,745
  • 15
  • 76
  • 136
  • 1
    can you show us some code? can you basically explain what you are trying to do? – JPro Feb 12 '10 at 15:00
  • 3
    @GZipp: **To all of you who are suggesting the use of `file_exists()`:** *checks whether a file **or directory** exists*. `is_file()` would be a better fit in this case. – Alix Axel Feb 12 '10 at 15:22
  • 1
    @Alix Axel: To all of you who are addressing me: I was suggesting nothing but that Smickie's assumption (that a relative path can't easily be transformed to a full path) was wrong. Others have pointed out that the full path isn't necessary; hence my comment rather than answer. – GZipp Feb 12 '10 at 15:43
  • @Alix: `is_file()` *is* better than `file_exists()` but isn't it even better to use `is_readable()`? – AgentConundrum Nov 24 '10 at 07:47
  • 1
    @AgentConundrum: `is_readable()`: "Returns TRUE if the file **or directory** specified by filename exists and is readable, FALSE otherwise". – Alix Axel Nov 24 '10 at 12:05

6 Answers6

59

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

Johannes Gorset
  • 8,345
  • 2
  • 34
  • 34
  • 2
    You don’t need the parentheses around the argument value of `include`. `include` is not a function but a language construct like `echo`. – Gumbo Feb 12 '10 at 15:17
  • 7
    @Gumbo I consider it good practice to use parantheses for language constructs, much like I do with `echo()` and `print()` as well. – Johannes Gorset Feb 12 '10 at 15:24
  • 2
    I'm not sure is this a good solution: you will not see fatal errors. – Michał Maluga Aug 23 '11 at 12:50
  • 1
    @Michal Malunga: I'm not sure that you understand the question. – Johannes Gorset Aug 24 '11 at 08:54
  • if you get an "cannot redeclare..." error when you remove the @-symbol in front of include try include_once instead. – nerdess Jan 08 '12 at 13:10
  • @Johannes Gorset: Michał pointed out a very valid problem. I use the approach above (with mute operator) as well for a JSON RPC mechanism. And sometimes scripts fail without ANY error in the logfile. Linting your scripts makes sure that they do not fail due to a (muted) syntax error. However, some issues can only be detected at runtime, for example a class that tries to override a parent's public constructor with a private one. Very easy to fix and see *IF* you remember to remove the mute operator for debugging. – Shi Aug 11 '12 at 13:09
  • 3
    It better to use `include_once` or `require_once` , this will be helpful while using `OOP` Concept and avoiding redeclaring of classes again. – Rafee Aug 23 '12 at 15:03
  • @Shi This is true, if someone uses this they'll have to be careful. I would mention though that in some situations it can be useful. My situation is that if a particular include_once does not occur, a class is missing so we need *more* action than the log file. If corrective measures dont work we send a message to the admin through their front-end user account. We also go ahead and throw a real error if a class is missing by require_once immediately after corrections. – Garet Claborn Sep 22 '13 at 09:08
10

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*/ }
Khawar
  • 137
  • 1
  • 5
9

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

Stephane JAIS
  • 1,365
  • 14
  • 13
  • A couple of user contributed notes here: stream_resolve_include_path() seems to cache it's output. After I renamed a file, I had to restart Apache for stream_resolve_include_path() to not return non-existing file name. This was on Windows. It really behaves like `include` and will only resolve the filename against the include-path, if the path is relative. It makes not much sense to resolve already absolute pathnames anyway. – RationalRabbit May 07 '19 at 01:41
6

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;
}
Yacoby
  • 51,022
  • 12
  • 106
  • 116
  • 1
    file_exists can't search in the include paths. You would have to parse them manually. – Petr Peller Feb 12 '10 at 15:06
  • 1
    PS == PATH_SEPARATOR && DS == DIRECTORY_SEPARATOR for those wondering, +1, hoping you would fix it, this works great and without overhead of exception and error handlings – mschr Jul 16 '12 at 10:18
5

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');
}
Alix Axel
  • 141,486
  • 84
  • 375
  • 483
1

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.

jacmkno
  • 997
  • 9
  • 21