I am trying to write data from form inputs to a text file using PHP. The file seems to load correctly, but does not write the data.
I have echoed the input values directly before trying to write to ensure they are not empty.
The text file is in the same directory as the php file.
I orginally got the error “Warning: fwrite(): supplied argument is not a valid stream resource” but corrected that by adjusting the permissions on the text file.
My current code
$fp = fopen('sidebar_subscribers.txt', 'a');
fwrite($fp, $name."\t".$email."\t".$leader."\t".$industry."\t".$country."\t".$zip."\r\n");
fclose($fp);
Why are the values not getting written to the text document?
Is there anything on the server that php needs to be set up for that I should check?
This is being used on a WordPress blog.
Thanks!
****** Issue is now resolved. The file was getting created and written in the wrong location. Thanks for all your help and suggestions. *******
The code as written works for me; I’d suggest adding the following to determine where the error is happening:
The original error about an invalid stream resource was a result of not checking your return values, which the “or die()” essentially does for you. fopen was failing, and returning false, and you were passing false as the first argument to fwrite.
You can further check for errors by examining the return value of fwrite: