如何在 PHP 中捕获 require() 或 include() 的错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8261756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to catch error of require() or include() in PHP?
提问by R_User
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 like to print an own error message, when it was not possible to include the code. Is it possible to execute one last command, if requeire did not work? the following did not work:
我正在用 PHP5 编写一个需要某些文件代码的脚本。当 A 文件不可用于包含时,首先会引发警告,然后会引发致命错误。当无法包含代码时,我想打印自己的错误消息。如果 requeire 不起作用,是否可以执行最后一个命令?以下不起作用:
require('fileERROR.php5') or die("Unable to load configuration file.");
Supressing all error messages using error_reporting(0)
only gives a white screen, not using error_reporting gives the PHP-Errors, which I don't want to show.
error_reporting(0)
仅使用抑制所有错误消息会产生白屏,不使用 error_reporting 会产生 PHP 错误,我不想显示。
回答by Sjan Evardsson
You can accomplish this by using set_error_handler
in conjunction with ErrorException
.
您可以通过set_error_handler
与 结合使用来完成此操作ErrorException
。
The example from the ErrorException
page is:
该ErrorException
页面的示例是:
<?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
}
?>
回答by Nicholas Rezmerski
I just use 'file_exists()':
我只使用'file_exists()':
if (file_exists("must_have.php")) {
require "must_have.php";
}
else {
echo "Please try back in five minutes...\n";
}
回答by ma?ek
A better approach would be to use realpathon the path first. realpath
will return false
if the file does not exist.
更好的方法是首先在路径上使用realpath。如果文件不存在,realpath
将返回false
。
$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 namespacethat wraps PHP's require function
您甚至可以在应用程序的命名空间中创建自己的 require 函数来包装 PHP 的 require 函数
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");
回答by Repox
I would suggest you took a look at the most recent commentin the documentation for the set_error_handler()function.
我建议您查看set_error_handler()函数文档中的最新评论。
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.
我还没有尝试过这个建议,但这可能会被用于其他致命的错误场景。
回答by Ondra Koupil
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.
您需要使用 include()。Require() 用于不存在的文件时,会产生致命错误并退出脚本,因此您的 die() 不会发生。Include() 只抛出警告,然后脚本继续。
回答by Prashant
A simple way I am using is that
我使用的一个简单方法是
<?php
...
if(!include 'config.php'){
die("File not found handler. >_<");
}
...
?>