tags:

views:

144

answers:

2
A: 

Try fflush($fp) before closing.

Ignacio Vazquez-Abrams
fclose always implicitly flushes the buffer.
meagar
Sorry, this did not work.
Joe
A: 

The code as written works for me; I'd suggest adding the following to determine where the error is happening:

<?php    
  $fp = fopen('sidebar_subscribers.txt', 'a') or die('fopen failed');

  fwrite($fp, "$name\t$email\t$leader\t$industry\t$country\t$zip\r\n") or die('fwrite failed');
?>

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:

<?php

$ret = fwrite($fp, "$name\t$email\t$leader\t$industry\t$country\t$zip\r\n");

if ($ret === false)
  die("Fwrite failed");
echo ("fwrite wrote $ret bytes");
?>
meagar
I did the above. No die errors returned, and it echoed that it wrote 50 bytes which seems pretty accurate for the small amount of data written. The text file is still blank though
Joe
Try changing the file mode to 'w' instead of 'a', see if anything appears in the file.
meagar
Also, try changing your filename to an absolute path, something like "/home/user/sidebar_subscribers.txt" and see if anything is written. It might be that you are indeed filling a file up with text, but it's a different file than the one you're checking.
meagar
Meagar you are exactly right. A new file was created in the root directory and was getting written to there. How would I specify absolute path, realpath('http://www.test.com/this/is/my/file/subscribe.txt) ?
Joe
@Joe You only need to specify a path to a file you have write permissions to, starting from "/", the root directory.
meagar
@Meagar, i set the path as "$fp = fopen('/wp-content/themes/sitename/sidebar_subscribers.txt', 'a');" and it is saying the file or directory does not exist. This is a WordPress install, is there anything i need to do different?
Joe
@Joe: Try `$fp = fopen($_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/sitename/sidebar_subscribers.txt', 'a');`
R. Bemrose
That worked! Wonderful. Thanks everyone for your help.
Joe