Thursday, July 23, 2009

Gotcha Of The Day: fopen(...) failed to open stream: Success

I'm updating a PHP app to generate a bunch of temporary files on the fly and ran into the oddest of error messages. I was basically saying:

  $stuff_name = tempnam("/tmp", "stuff-");
  $stuff_handle = fopen($stuff_name, "+w");

And was getting the error message:

Error Message: fopen(/tmp/stuff-K6Y5qv) [function.fopen]: failed to open stream: Success

Let me get this straight, there's an error, and yet the detail is Success. Huh?

Googling didn't help. So I went back and trip checked my work. And of course, there was an error. I said +w when I meant to say w+. D'oh! This code works:

  $stuff_name = tempnam("/tmp", "stuff-");
  $stuff_handle = fopen($stuff_name, "w+");

1 comment:

  1. Anonymous9:31 PM

    The Internet is so incredible, my error was a comma ',w+', after 6 hours of googling i get the answer in your 6 years old comment. :-)

    ReplyDelete