[Help]Writing to text file
Well I have this simple form which writes text to a text file. Everything works fine, but when a new message is submitted it overwrites the previous one.
This is the code I wrote
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Your name: <br />
<input type="text" name="name" size="15" />
<br />
Your message:<br />
<input type="text" name="msg" size="35" />
<br />
<input type="submit" value="Go!" />
</form>
<br />
<?php
$name = $_POST['name'];
$msg = $_POST['msg'];
$file = 'message.txt';
// open file
$fh = fopen($file, 'w') or die('Could not open file!');
// write to file
fwrite($fh, "
$name:
$msg") or die('Could not write to file');
// close file
fclose($fh);
?>
</body>
</html>
Anyone know how to make it so that when a new message is submitted, it writes it under the last message that is made?
Re: [Help]Writing to text file
It's actually replacing the entire folder and not the msg (samething basically) gimme a sec and I'll figure it out ^_^
PHP Code:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Your name: <br />
<input type="text" name="name" size="15" />
<br />
Your message:<br />
<input type="text" name="msg" size="35" />
<br />
<input type="submit" value="Go!" />
</form>
<br />
<?php
$name = $_POST['name'];
$msg = $_POST['msg'];
$file = 'message.txt';
if(isset($_POST['name']) && isset($_POST['msg'])) {
$previous_content = file_get_contents($file);
$file_contents = "Name: $name
Msg: $msg
".$previous_contents;
// write to file
file_put_contents($file, $file_contents, FILE_APPEND) or die('Could not write to file');
// close file
fclose($fh);
}
else
echo "Enter your msg";
?>
Tuhh duhh!
fopen() creates a new file every time ^_^
Now Wh005h learned something too hehe
Re: [Help]Writing to text file
Ah thanks Wh005h! ^^
EDIT: Uh, your method doesn't write anything do the file at all. O.o
Re: [Help]Writing to text file
Works dandy for me...
That code I posted is the entire page-all you need for it.
Check and make sure something you have precoded aside from that snippet that might be messing with it. It works for me :?
**Edit**
Found two things 'unneeded' though
PHP Code:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Your name: <br />
<input type="text" name="name" size="15" />
<br />
Your message:<br />
<input type="text" name="msg" size="35" />
<br />
<input type="submit" value="Go!" />
</form>
<br />
<?php
$name = $_POST['name'];
$msg = $_POST['msg'];
$file = 'message.txt';
if(isset($_POST['name']) && isset($_POST['msg'])) {
$file_contents = "Name: $name
Msg: $msg
";
// write to file
file_put_contents($file, $file_contents, FILE_APPEND) or die('Could not write to file');
echo "Msg written";
}
else
echo "Enter your msg";
?>
I'm still learning too lol
Re: [Help]Writing to text file
1. Read the manual
2. Note that the 'a+' modifier does exactly what you want:
Quote:
'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
Which is far neater then first reading the entire file and place it in the buffer.
3. Discover you actually never bothered to read either the manual or the rules
4. Get an infraction and topic lock.