如何输出PHP文件打开失败的原因

发布于 2024-07-29 21:37:02 字数 339 浏览 4 评论 0原文

我正在尝试调试一个巨大的、过时的(大约 2001 年)PHP Web 服务,但遇到文件打开失败的情况。 fopen 调用位于包含的模块中,调用者正在记录文件无法打开,但没有记录任何原因。

实际执行打开操作的代码是:

  // Read the file
  if (!($fp = @fopen($fileName, 'rb'))) {
    $errStr = "Failed to open '{$fileName}' for read.";
    break; // try-block
  }

How can I find why fopen failed?

I'm trying to debug a huge, antiquated (circa 2001) PHP web service and I'm encountering file open failures. The fopen call is in an included module, the caller is logging that the file could not be opened but no reason is being logged.

The code that actually does the open is:

  // Read the file
  if (!($fp = @fopen($fileName, 'rb'))) {
    $errStr = "Failed to open '{$fileName}' for read.";
    break; // try-block
  }

How can I find out why fopen failed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

始于初秋 2024-08-05 21:37:04

关于 @ 运算符,已经给出了很好的答案,但这里是更多信息可能对您或其他人有用:

  • 如果出于调试目的,您需要禁用 @ 运算符,您可以安装 scream 扩展 -- 另请参阅 手册 -- 当您维护某种设计/编码不佳的旧应用程序时,这非常有用 ^^
  • 取决于您的 PHP 配置(如果 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);
}

你会得到这样的:

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)

所以,真实的,有用的,有意义的错误消息;-)

Great answers have already been given, about the @ operator, but here's a couple of more informations that could be useful, either to you or someone else :

  • If, for debugging purposes, you need the disable the @ operator, you can install the scream extension -- see also the manual -- which is really useful when you're maintaining some kind of old application not well designed / coded ^^
  • Depending on your PHP configuation (if the track_errors option is activated), you might be able to use $php_errormsg to get the last error message.

Considering this piece of code :

// 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);
}

You would get this :

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)

So, real, useful, meaningful, error messages ;-)

三生一梦 2024-08-05 21:37:03

去掉@符号。

@ 符号抑制错误消息,因此它抑制了函数通常会给出的错误。

Take away the @ sign.

The @ sign suppresses error messages, so it is supressing the error the the function would normally give.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文