codeboy89 Posted July 8, 2010 Share Posted July 8, 2010 i have the following code for taking input data and writing it to a textfile then echoing it to a webpage to view. i do not know why sometimes data is left over from a previous submission and sometimes it clears it. i want it to clear out the textfile and only show the last inputed message when i echo it. $file = "msg.txt"; $read = file_get_contents($file); $open = fopen($file, "r+"); $lock = flock($open, LOCK_EX); $add = "$msg"; $add = fwrite($open, "$add"); fclose($open); please help Link to comment https://forums.phpfreaks.com/topic/207095-help-with-writingviewing-text/ Share on other sites More sharing options...
kenrbnsn Posted July 8, 2010 Share Posted July 8, 2010 Where is $msg coming from? Also, you're using too many variables, just do: <?php $file = "msg.txt"; $open = fopen($file, "w"); $lock = flock($open, LOCK_EX); file_put_contents($msg); fclose($open); ?> Ken Link to comment https://forums.phpfreaks.com/topic/207095-help-with-writingviewing-text/#findComment-1082868 Share on other sites More sharing options...
codeboy89 Posted July 8, 2010 Author Share Posted July 8, 2010 $msg is input text data from the user and i want it posted, but if someone else inputs text data it displays only the new entry. currently it is leaving up the old data also.. Link to comment https://forums.phpfreaks.com/topic/207095-help-with-writingviewing-text/#findComment-1083274 Share on other sites More sharing options...
AbraCadaver Posted July 8, 2010 Share Posted July 8, 2010 $file = "msg.txt"; file_put_contents($file, $msg); Link to comment https://forums.phpfreaks.com/topic/207095-help-with-writingviewing-text/#findComment-1083278 Share on other sites More sharing options...
AbraCadaver Posted July 8, 2010 Share Posted July 8, 2010 $file = "msg.txt"; file_put_contents($file, $msg); Might also use the LOCK_EX flag for file_put_contents() if needed. Link to comment https://forums.phpfreaks.com/topic/207095-help-with-writingviewing-text/#findComment-1083279 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.