如何输出PHP文件打开失败的原因
我试图调试一个巨大的、过时的(大约2001年)PHP web服务,但遇到了文件打开失败的问题。fopen调用位于包含的模块中,调用方记录文件无法打开,但没有记录任何原因 实际执行打开操作的代码是:如何输出PHP文件打开失败的原因,php,fopen,error-reporting,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 } 我怎样才能
// Read the file
if (!($fp = @fopen($fileName, 'rb'))) {
$errStr = "Failed to open '{$fileName}' for read.";
break; // try-block
}
我怎样才能找到fopen失败的原因?去掉@符号
@sign会抑制错误消息,因此它会抑制函数通常会给出的错误。删除。关于的,已经给出了很好的答案,但这里还有一些可能对您或其他人有用的信息:
- 如果出于调试目的,您需要禁用
,那么您可以安装--(另请参见--)当您维护一些设计/编码不好的旧应用程序时,这非常有用^^@操作符
- 根据您的PHP配置(如果该选项被激活),您可能可以使用来获取最后一条错误消息
// 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);
}
你会得到这个:
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)
string'fopen(/tmp/non-existant file.txt)[]:无法打开流:没有这样的文件或目录(长度=129)
字符串“fopen(/tmp/vboxdrv Module.symvers)[]:无法打开流:权限被拒绝”(长度=122)
因此,真实、有用、有意义的错误消息;-)强>