Bluehost.com Web Hosting $6.95

append text to a file

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 ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2006
acord
 
Posts: n/a
Default append text to a file

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
Reply With Quote
  #2 (permalink)  
Old 03-01-2006
lain
 
Posts: n/a
Default Re: append text to a file

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

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:45 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0