Connection Coder Connection Coder - 1 year ago 22
PHP Question

My fopen function works, but the fwrite function doesn't

So I'm trying to create a file and write code into that file whenever a user submits a register and successfully moves on to the activation stage. I'm doing this so that I can store all of the variables and information in my registration php file into the file I create. This is relevant code of the signup form:

#$file is set in removed code
$filename = '../' . $file;
fopen($filename, "w") or die("<h1 style='text-align: center; color: red;'>There has been an error creating your user files. Try again later.</h1>");
$content = "


fwrite($filename, $content);

Everything works, except for the
function. I looked at the file I created, and nothing appears in it. What's going on?


fopen() returns a stream resource bound to $filename. When you call fwrite(), the first parameter it takes is the resource returned by fopen(). Not the filename.

So change the relevant part of your program to this:

$handle = fopen($filename, "w") or die("...");
$content = "foobar";
fwrite($handle,  $content);
fclose($handle); // Don't forget to close when you're done.