swamp Posted February 23, 2009 Share Posted February 23, 2009 Hi, I need to write to a txt file but leave the last line intact. I'm using fwrite($handle, $content) but I need to keep the last line in my txt file which is "--Document End--" For example if I write "new line of text" to "test.txt" I need it to show up as (old content) new line of text --Document End-- Any hints? Thanks Link to comment https://forums.phpfreaks.com/topic/146523-fwrite-above-last-line/ Share on other sites More sharing options...
sloth456 Posted February 23, 2009 Share Posted February 23, 2009 yeah, just make sure when you update the file that you first remove the old --Document End-- and post a new --Document End-- at the bottom. Is that a satisfactory solution? Link to comment https://forums.phpfreaks.com/topic/146523-fwrite-above-last-line/#findComment-769290 Share on other sites More sharing options...
swamp Posted February 23, 2009 Author Share Posted February 23, 2009 ... I know the logic of it, I'm just not sure about the code, any hints on how to do that? Link to comment https://forums.phpfreaks.com/topic/146523-fwrite-above-last-line/#findComment-769301 Share on other sites More sharing options...
sloth456 Posted February 23, 2009 Share Posted February 23, 2009 Do an fopen statement and put what you got into a string called $text. Then do a str_replace to replace --Document End-- with nothing. Then something like this $newtext=$text.$newline."--Document End--"; Then rewrite the old file with $newtext. Link to comment https://forums.phpfreaks.com/topic/146523-fwrite-above-last-line/#findComment-769315 Share on other sites More sharing options...
swamp Posted February 23, 2009 Author Share Posted February 23, 2009 Cheers Link to comment https://forums.phpfreaks.com/topic/146523-fwrite-above-last-line/#findComment-769352 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.