Re: SV: [PHP] fopen "r+" not working - how to add to beginning of txt file

From: Date: Tue, 20 Nov 2001 05:42:19 +0000
Subject: Re: SV: [PHP] fopen "r+" not working - how to add to beginning of txt file
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-75210@lists.php.net to get a copy of this message
On Tue, 20 Nov 2001 14:41, Brian Tully wrote: > On 11/19/01 6:45 PM, "Phelps" <phelps@swipnet.se> wrote: > > load the content of the file into an variable first and then add the > > new text part into it. > > > > something like this: > > > > $text = $newtext + $text; > > > > then save it... It should work... > > > > P. > > > >> -----Ursprungligt meddelande----- > >> Från: Brian Tully [mailto:btully@gstockco.com] > >> Skickat: den 19 november 2001 22:03 > >> Till: PHP > >> Ämne: [PHP] fopen "r+" not working - how to add to beginning of txt > >> file > >> > >> > >> hey there - > >> > >> i've been going crazy trying to figure out how to add news items > >> to the top > >> of an already existing txt file containng previous news posts. While > >> I'd love to change the system so everything is database-driven (as > >> most of the rest of the site is), this news area has so much news > >> posts that it would take forever to convert the info the database. > >> > >> SO... i want to just create a simple script that adds text to the > >> beginning > >> of an existing text file, without overwriting any previous text > >> in the file. > >> The manual and php site refer to using the fopen function with > >> the r+ option > >> - which i've done, BUT some if the text still gets overwritten > >> with the new > >> next. > >> > >> here's what I'm using: > >> > >> <?php > >> > >> $today = getdate(); > >> $month = $today[month]; > >> $mday = $today[mday]; > >> $year = $today[year]; > >> > >> > >> if ($fp = fopen("/home/foo/www/whats_new/whats_new2.txt", "r+")) { > >> > >> fwrite($fp, "this is the new line of text I want to add to the > >> top of the page"); > >> > >> fclose($fp); > >> > >> print "success!"; > >> > >> } > >> > >> else { > >> > >> print "Could not open What\'s New file for updating."; > >> > >> } > >> > >> ?> > >> > >> > >> What happens is that the line does get written to the top of the > >> page but it > >> overwrites or deletes some of the existing text. Even though the r+ > >> option is supposed to put the pointer at the beginning of the file > >> and add to it as > >> opposed to overwriting it doesn't seem to be working for me. > >> > >> What am I doing wrong? > >> > >> Is there a better way to accomplish what I'm trying to do? > >> > >> any help would be GREATLY appreciated! > >> > >> regards, > >> brian > not sure I know how to do this... > > do you mean to open the file twice? once to read it and put it into a > variable and then close the file and open it again, this time for > writing? > > I'm confused. this seems so much harder than it would be with Perl. But > I really want to stick with PHP. Can anyone help me out here? I've > looked the manual over and over, and checked the php.net website, but > nothing covers the situation where you want to add text to the TOP of a > file. fopen with the r+ option is not working the way it should. > > I'd be eternally grateful... > > brian >-- David Robley Techno-JoaT, Web Maintainer, Mail List Admin, etc CENTRE FOR INJURY STUDIES Flinders University, SOUTH AUSTRALIA (C) 1992 Wild Bill's Machine Gun Shop and House of Wax.

« previous php.general (#75210) next »