How to catch error of require() or include() in PHP?

前端 未结 6 1020
甜味超标
甜味超标 2020-12-08 15:33

I\'m writing a script in PHP5 that requires the code of certain files. When A file is not available for inclusion, first a warning and then a fatal error are thrown. I\'d li

相关标签:
6条回答
  • 2020-12-08 16:04

    I just use 'file_exists()':

    if (file_exists("must_have.php")) {
        require "must_have.php";
    }
    else {
        echo "Please try back in five minutes...\n";
    }
    
    0 讨论(0)
  • 2020-12-08 16:15

    You can accomplish this by using set_error_handler in conjunction with ErrorException.

    The example from the ErrorException page is:

    <?php
    function exception_error_handler($errno, $errstr, $errfile, $errline ) {
        throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
    }
    set_error_handler("exception_error_handler");
    
    /* Trigger exception */
    strpos();
    ?>
    

    Once you have errors being handled as exceptions you can do something like:

    <?php
    try {
        include 'fileERROR.php5';
    } catch (ErrorException $ex) {
        echo "Unable to load configuration file.";
        // you can exit or die here if you prefer - also you can log your error,
        // or any other steps you wish to take
    }
    ?>
    
    0 讨论(0)
  • 2020-12-08 16:15

    You need to use include(). Require(), when used on non-existent file, produces a fatal error and exits the script, so your die() won't happen. Include() only throws warning and then the script continues.

    0 讨论(0)
  • 2020-12-08 16:23

    A simple way I am using is that

    <?php
        ...
        if(!include 'config.php'){
            die("File not found handler. >_<");
        }
       ...
    ?>
    
    0 讨论(0)
  • 2020-12-08 16:24

    A better approach would be to use realpath on the path first. realpath will return false if the file does not exist.

    $filename = realpath(getcwd() . "/fileERROR.php5");
    $filename && return require($filename);
    trigger_error("Could not find file {$filename}", E_USER_ERROR);
    

    You could even create your own require function in your app's namespace that wraps PHP's require function

    namespace app;
    
    function require_safe($filename) {
      $path = realpath(getcwd() . $filename);
      $path && return require($path);
      trigger_error("Could not find file {$path}", E_USER_ERROR);
    }
    

    Now you can use it anywhere in your files

    namespace app;
    
    require_safe("fileERROR.php5");
    
    0 讨论(0)
  • 2020-12-08 16:27

    I would suggest you took a look at the most recent comment in the documentation for the set_error_handler() function.

    It suggests the following as a method (and with an example) of catching fatal errors:

    <?php
    function shutdown()
    {
        $a=error_get_last();
        if($a==null)  
            echo "No errors";
        else
             print_r($a);
    
    }
    register_shutdown_function('shutdown');
    ini_set('max_execution_time',1 );
    sleep(3);
    ?> 
    

    I haven't tried the suggestion, but this could propably be used in other fatal error scenarios.

    0 讨论(0)
提交回复
热议问题