Detailed error on fopen

后端 未结 4 729
深忆病人
深忆病人 2021-01-01 12:13

I\'m using fopen to read from a file

$fh = fopen($path, \'r\') or die(\'Could not open file\');

Now I contantly get error Could not open fi

相关标签:
4条回答
  • 2021-01-01 12:26
    $fh = fopen($path, 'r') or  die (error_get_last());
    
    0 讨论(0)
  • 2021-01-01 12:30

    For php versions prior to 5.2 (lacking error_get_last()) you can use track_errors.

    ini_set('track_errors', 1);
    $fh = fopen('lalala', 'r');
    if ( !$fh ) {
      echo 'fopen failed. reason: ', $php_errormsg;
    }
    

    see also: http://de.php.net/reserved.variables.phperrormsg

    0 讨论(0)
  • 2021-01-01 12:38

    Turn on error reporting, or, in a production environment (from PHP 5.2.0 onwards) you should also be able to use error_get_last().

    0 讨论(0)
  • 2021-01-01 12:41

    Yes.
    PHP has detailed error message for you.
    You just have to turn it on.

    To dislay it on the screen add these 2 lines at the top of the script:

    ini_set('display_errors',1);
    error_reporting(E_ALL);
    

    Or if you want it to be logged instead,

    ini_set('log_errors',1);
    ini_set('display_errors',0);
    error_reporting(E_ALL);
    

    Also note that using die() is very bad practice.

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