How to output the reason for a PHP file open failure

user24989 picture user24989 · Jul 30, 2009 · Viewed 9.6k times · Source

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?

Answer

Tyler Carter picture Tyler Carter · Jul 30, 2009

Take away the @ sign.

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