我不能像这样做吗?
try {
require_once( '/includes/functions.php' );
}
catch(Exception $e) {
echo "Message : " . $e->getMessage();
echo "Code : " . $e->getCode();
}
没有错误信息被回显,服务器返回500。
我不能像这样做吗?
try {
require_once( '/includes/functions.php' );
}
catch(Exception $e) {
echo "Message : " . $e->getMessage();
echo "Code : " . $e->getCode();
}
没有错误信息被回显,服务器返回500。
你可以使用 include_once
或者 file_exists
来实现:
try {
if (! @include_once( '/includes/functions.php' )) // @ - to suppress warnings,
// you can also use error_reporting function for the same purpose which may be a better option
throw new Exception ('functions.php does not exist');
// or
if (!file_exists('/includes/functions.php' ))
throw new Exception ('functions.php does not exist');
else
require_once('/includes/functions.php' );
}
catch(Exception $e) {
echo "Message : " . $e->getMessage();
echo "Code : " . $e->getCode();
}
如您可以在这里所读到的:(强调是我的)
require()与include()相同, 只有在失败时会产生致命的E_COMPILE_ERROR级错误。 换句话说,它将停止脚本。
这是关于require的,但是它等同于require_once()。这不是可捕获的错误。
顺便说一下,您需要输入绝对路径,我认为下面这个不正确:
require_once( '/includes/functions.php' );
你可能想要像这样的东西
require_once( './includes/functions.php' );
或者,如果你是从子目录中调用此函数,或者从包含在不同目录中的文件中调用它,你可能需要像这样的东西:
require_once( '/var/www/yourPath/includes/functions.php' );
这个方法应该可以用,但有点取巧。
if(!@include_once("path/to/script.php")) {
//Logic here
}