This is a discussion on append text to a file within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi, I have tried with many options, eg. w+, a+, a, but none of them can append the text to ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi,
I have tried with many options, eg. w+, a+, a, but none of them can append the text to the end of the file. This is the function I used for appending message to the file (xml_test.txt) function dump_to_file($var) { $fh = fopen("/tmp/xml_test.txt", "a"); fwrite($fh, "\n----------\n"); fwrite($fh, $var); fclose($fh); } What s the right way to append a message to the end of the file in PHP4/5 ? Thanks Sam |
|
|||
|
Hey Sam,
the mentioned functions "can" append text to a file. Maybe you can explain, what exactly they cannot? Is the filehandler ($fh) maybe null? Was there any error open the file (rights)? the easier way as of php5 might be: $old_content = file_get_contents('filename'); $new_content = $old_content . "\n" . "i love strings"; file_put_contents('filename', $new_content); if you only have problems with the append but can totally rewrite the file: file_get_contents is also available in php > 4.3.0. Append your new content to its return string and write it to the file |