use*_*989 9 php fopen error-reporting
我正在尝试调试一个庞大的,过时的(大约2001年)PHP Web服务,我遇到了文件打开失败.fopen调用在一个包含的模块中,调用者记录该文件无法打开但没有记录任何原因.
实际打开的代码是:
// Read the file
if (!($fp = @fopen($fileName, 'rb'))) {
$errStr = "Failed to open '{$fileName}' for read.";
break; // try-block
}
Run Code Online (Sandbox Code Playgroud)
我如何才能找出fopen失败的原因?
关于@ 运算符,已经给出了很好的答案,但是这里还有一些可能对您或其他人有用的信息:
@ operator,您可以安装尖叫扩展 - 另请参阅手册-当您维护某种设计/编码不佳的旧应用程序时,这非常有用 ^^track_errors激活该选项),您也许能够使用它$php_errormsg来获取最后的错误消息。考虑这段代码:
// This file doesn't exist
if (!@fopen('/tmp/non-existant-file.txt', 'r')) {
var_dump($php_errormsg);
}
// My Apache server doesn't have the right to read this file
if (!@fopen('/tmp/vboxdrv-Module.symvers', 'w')) {
var_dump($php_errormsg);
}
Run Code Online (Sandbox Code Playgroud)
你会得到这个:
string 'fopen(/tmp/non-existant-file.txt) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: No such file or directory' (length=129)
string 'fopen(/tmp/vboxdrv-Module.symvers) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: Permission denied' (length=122)
Run Code Online (Sandbox Code Playgroud)
所以,真实的、有用的、有意义的错误消息;-)