Connection Coder Connection Coder - 7 months ago 8
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 = "
<?php

potato

?>
";
fwrite($filename, $content);


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

Answer

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.
Comments