PHP when writing to a file, How to add and add text to a file with existing text?

I am creating an xml file with PHP, here is a sample code.

$myFile = "example_file.xml"; $fh = fopen($myFile, 'w'); while($row = mysql_fetch_array($result)) { $stringData = "<field name=\"id\">$page_id</field> <field name=\"url\">http://myfundi.co.za/a/$page_url</field> <field name=\"title\">$pagetitle</field> <field name=\"content\">$bodytext</field> <field name=\"site\">Myfundi</field>"; fwrite($fh, $stringData); } fclose($fh); 

I need to do when the first text is written to a text file, I need to add and add more text.

I need to add and add to existing data.

How can i do this?

thanks

+4
source share
3 answers

You cannot directly add text to a file. The only practical method is to open a new temporary file, write out new text, and then copy the source text to the end.

+6
source

You cannot add a file. Read the contents, add new text and write it to your file. If you want to add something, just open the file with the flag "a" and write in it. See http://php.net/manual/de/function.fopen.php

+1
source

Try the following:

http://php.net/manual/en/function.fwrite.php

Hope this helps.

0
source

All Articles