Hi everyone 🙂 First time poster. I've read the guidelines, and searched for this problem in the forums.
Here's my setup and subsequent problem: I'm making a "blog" utility so that I don't have to hand-code all the repetitive HTML formatting. I have an entry-form page that posts its contents to the published blog site. There is a thread similar to this about a newsfeed page and I've used some of the ideas from that thread to modify my code to append to the beginning, but I'm getting the same problem. What's happening is part of the string is written twice to the file! I've echoed the string before and after the fwrite() command, and the echoed string comes out fine, but once its written there's a double. Before I continue, here's the code:
<?php
$title = $_POST["title"];
$content = $_POST["content"];
$location = "./frontpage.txt";
$curDate = date("l dS of F Y");
$cmntDate = "<!---------".$curDate."----------->";
$string = nl2br($cmntDate.$content);
$string .= file_get_contents($location);
echo $string;
if (!($fp = fopen($location, "w+"))) { echo 'Cannot open file'; }
$write = fwrite($fp, $string);
fclose($fp);
?>
As you can see, I'm trying to add a commented out Date label to the contents of my post. Everything seems to work fine, since the "echo $string" produces exactly what I want. But when I open the file, the Date label string is posted twice! For example, let's say I put "Today I went to the store." in the content box of my blog-entry page and clicked "submit". The echo would, and should, display:
<!-------- Wednesday July 28, 2004------>Today I went to the store.
What I'm getting in the file is:
<!-------- Wednesday July 28, 2004------><!-------- Wednesday July 28, 2004------>Today I went to the store.
One thing you might think is, "Well, you should try opening in r+ mode, or w+ so you can do all your read/writes at once. Well, I tried that, and was getting exponential amounts of the content I wrote! At first it would be double, then quadruple, then .... you get the idea. Only by separating into "r" and "w" snippets, was I able to get as close as I am now (though the newsfeed thread rid me of opening the file in "r" mode and replacing that with a file_get_contents. Thanks). I've tried many combinations with dotting together variables, like taking the $cmntDate out of the nl2br parameter, opening with b and t switches, etc. I think it has something to do with the fwrite() command.
Any ideas? I've been slaving at this (seemingly simple) application for a day and a half! ARGH! 😕:mad: