Brian W Posted September 23, 2008 Share Posted September 23, 2008 I have a form that Posts to a .php with the code below. I want it to take the original content of the file and then add what I posted ontop. Right now it replaces the original text. <?php $handle = fopen('Games/Data1.html', 'w+'); $Read = fread('Games/Data1.html'); $Text = $_POST['input']."<br/>".$Read; ?> <?php fwrite($handle, $Text) ?> <?php fclose('Games/Data1.html'); header('location:Table.php')?> I'm probably using the fread incorrectly. I am using PHP 4 I believe. BTW, if you didn't gather, this kinda works like a chat file because it kinda is. lol but not exactly. Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 23, 2008 Author Share Posted September 23, 2008 First off, i've made three changes to the code so far. 2 of which I modified on the above post. the latest if fixed me dumb a** mistake that should have never even happened. $Read = fread($handle);//thats a little better, but for all I know may still be incorrect Still not adding the string on top, just rewriting the whole file with what i post. Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 23, 2008 Share Posted September 23, 2008 Check out fopen in the manual... More specifically the 'mode' argument Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 23, 2008 Author Share Posted September 23, 2008 OH, thank you... I read "truncate to 0" and thought, "that doesn't good". So, I looked into it. Bad. redesigned code= <?php $filename = 'Games/Data1.html'; $handle = fopen($filename, 'x'); fclose($handle); $handle = fopen($filename, 'r+'); $contents = fread($handle,"20000"); $Text = $_POST['input']."<br/>"; ?> <?php fwrite($handle, $Text); ?> <?php fclose($handle); header('location:Table.php');?> It is now not saving over, which is nice. I have the x one there to create the file if it doesn't exist. Now, how am I supposed to point the write to the beginning? Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 23, 2008 Author Share Posted September 23, 2008 Nevermind, because I left the piece in there that read, it pointed to the end. cut that part and it worked. Thanks again. Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 23, 2008 Share Posted September 23, 2008 The 'x' modeshould place the pointer at the beginning of the file.. so any fwrites should automatically go above... no need to fread(). <?php $filename = 'Games/Data1.html'; $handle = fopen($filename, 'x'); $Text = $_POST['input']."<br/>"; ?> fwrite($handle, $Text); fclose($handle); header('location:Table.php');?> Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 23, 2008 Author Share Posted September 23, 2008 the description of 'x' made mention of that if the file already exists, it would return FALSE. I thought that meant its only good for creating brand new files... Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 23, 2008 Share Posted September 23, 2008 It sure does... You should use 'r+' My bad Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 23, 2008 Author Share Posted September 23, 2008 I don't need to read, least I don't think I do. NEW PROBLEM OCCURRED Just as it started working. Suddenly it bugged out and stopped. WTF, really. It was odd because it was working and then I opened 2 windows so I could chat with myself and suddenly it glitched and won't work anymore. What it does now is replace what I have written, like when you hit the insert key (oh how I hate that key). Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 23, 2008 Share Posted September 23, 2008 r+ is for reading and writing. It's the only way you'll get your pointer at the start of the file without loading the contents into memory. Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 23, 2008 Author Share Posted September 23, 2008 rewind() my friend. I figured out the problem and got it working, or at least it works. Like I said, it was writting over anything over what was already there. So, I had it read, then write my new stuff with what it read before attached to the end of it, essentially adding content every time. Topic solved, lesson learned. Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 23, 2008 Share Posted September 23, 2008 Ah, first time i've seen that function. Good to know. Quote Link to comment Share on other sites More sharing options...
Brian W Posted September 23, 2008 Author Share Posted September 23, 2008 This is the ugly piece of crap I made. It works <?php $filename = 'Games/Data1.html'; $handle = fopen($filename, 'x'); fclose($handle); $handle = fopen($filename, 'r+'); $content = fread($handle, filesize($filename)); rewind($handle); $Text = $_POST['input']."<br>".$content; ?> <?php fwrite($handle, $Text); ?> <?php fclose($handle); header('location:Table.php');?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.