Jump to content

fopen("w") creates file then fwrite on handle cause "Invalid Stream"


Recommended Posts

I'm trying to write debugging data to a file.  I open the file:

$dfile = fopen("debug/PHPdebug.txt","w") || die; 

The process continues, so $dfile is a valid handle (???)

 

Next I write to the file:

fwrite($dfile, "this data will not appear\n");

 

However, that data does not appear, and in the apache error log I get "PHP Warning:  fwrite(): supplied argument is not a valid stream resource".

 

The directory "debug" is owned by apache and set 755 (I've tried 777 too).  The file does get created each time. Just to be clear:  the fopen works, but the fwrite on the handle returned does not.

 

Why can I open a file and then not use the handle?  Googling for this issue is proving useless, since it's all people who's fopen() also failed because of permissions or whatever.

 

 

 

Use OR instead of ||

 

Because || has a lower precedence than =, your code is or'ing the result of fopen() with die; (which is nothing) and assigning that to $dfile.

 

If you had error_reporting set to E_ALL, you would also see the following Warning that would help you track down what your code is doing -

 

Warning: fwrite() expects parameter 1 to be resource, boolean given in your_file.php on line x

OR instead of || (is lower than =) eh? LOL... That would explain why the value of $dfile was 1 when the file opened but zilch when the permissions were wrong.  Heh-heh.

 

Thanks much  PFMaBiSmAd.  I've set error_reporting to E_ALL, too, the more the merrier.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.