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. Link to comment https://forums.phpfreaks.com/topic/125521-solved-fread/ 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. Link to comment https://forums.phpfreaks.com/topic/125521-solved-fread/#findComment-648998 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 Link to comment https://forums.phpfreaks.com/topic/125521-solved-fread/#findComment-649047 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? Link to comment https://forums.phpfreaks.com/topic/125521-solved-fread/#findComment-649060 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. Link to comment https://forums.phpfreaks.com/topic/125521-solved-fread/#findComment-649062 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');?> Link to comment https://forums.phpfreaks.com/topic/125521-solved-fread/#findComment-649065 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... Link to comment https://forums.phpfreaks.com/topic/125521-solved-fread/#findComment-649068 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 Link to comment https://forums.phpfreaks.com/topic/125521-solved-fread/#findComment-649083 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). Link to comment https://forums.phpfreaks.com/topic/125521-solved-fread/#findComment-649085 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. Link to comment https://forums.phpfreaks.com/topic/125521-solved-fread/#findComment-649117 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. Link to comment https://forums.phpfreaks.com/topic/125521-solved-fread/#findComment-649122 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. Link to comment https://forums.phpfreaks.com/topic/125521-solved-fread/#findComment-649124 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');?> Link to comment https://forums.phpfreaks.com/topic/125521-solved-fread/#findComment-649128 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.