Closed Thread Icon

Preserved Topic: append text to text file using php (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=21241" title="Pages that link to Preserved Topic: append text to text file using php (Page 1 of 1)" rel="nofollow" >Preserved Topic: append text to text file using php <span class="small">(Page 1 of 1)</span>\

 
shattered
Obsessive-Compulsive (I) Inmate

From:
Insane since: Feb 2003

posted posted 03-13-2003 10:47

Hi guys,
if you would look at http://www.radio.deltapath.com/dedicationmail.php , notice the "add a dedication" field. Each time a user submits a dedication, the dedication would be appended to a text file called dedicationmsg.txt . But with what I have right now, it always erases my previous message and replace it with the new submitted message. How do I get it to append?
This is my code below:

code:
<?
if ($submit){
$insert="INSERT INTO Request (message) VALUES ('$message')";
$result=mysql_query($insert) or die ('Could not insert data.');
}

if($file=fopen("/home/omis107/dedicationmsg.txt", "w+")){
fputs($file, "$name to $name1 $message");
fclose($file);

}


$new_value_to_insert = "$message";
$content = file ($file);

array_unshift($content, $new_value_to_insert );

$fp = fopen($file, "w");
foreach($content as $message){ fwrite($fp, $message); }
fwrite($fp, $message);
fclose($fp);


?>




Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 03-13-2003 10:52

shattered: I'd guess you need to read the current contents add the new dedication to that and then write the lot back to the flat file (this has come up a lot before so search this forum). I'm not sure why you are also inserting data into a database though

___________________
Emps

FAQs: Emperor

shattered
Obsessive-Compulsive (I) Inmate

From:
Insane since: Feb 2003

posted posted 03-13-2003 11:38

Thank you for your help Emperor!
Found out that I need to use "a+" instead of "w+" and that solved the problem!

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 03-13-2003 18:29

Yes, I am also confused as to why you are entering data into the DB and adding it to a text file

It's usually an 'either or' kind of situation.

If the data is going into the DB, it can be pulled into the file whenever you need it without having to actually open/write/save it.

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 03-14-2003 02:33

Change the line that reads
if($file=fopen("/home/omis107/dedicationmsg.txt", "w+")){

to
if($file=fopen("/home/omis107/dedicationmsg.txt", "a")){

notice the "a" instead of the "w+". You want to append and not write.

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 03-14-2003 09:09

Sorry Pugz...you got in a bit late with that otherwise helpful piece of advice

quote:
Found out that I need to use "a+" instead of "w+" and that solved the problem!



« BackwardsOnwards »

Show Forum Drop Down Menu