On Tue, 20 Nov 2001 14:41, Brian Tully wrote: > 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
I think you misunderstand the r+ flag. You said > >> 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. "r+’ - Open for reading and writing; place the file pointer at the beginning of the file." Now, nowhere does the docs say 'and add to it'; what happens is that the pointer is placed at the beginning of the file _and anything written to the file is overwritten there_. You just can't insert lines in a file like that; and that isn't a restriction of PHP, it's the way OSes in general work. So if you want to add a line at the beginning of the file, you need to read the entire file into a variable, prepend the stuff you want to add and then write out the whole lot to the file. You could probably simplify the process a bit by using file() to read the file into an array, then adding that array to the array of stuff you want at the beginning of the file and then writing the whole array out to file; that way you only have to do one fopen. I dunno how you would do it in PERL, but the principle is the same at the coalface - perhaps PERL hides a bit of the work from you?? -- David Robley Techno-JoaT, Web Maintainer, Mail List Admin, etc CENTRE FOR INJURY STUDIES Flinders University, SOUTH AUSTRALIA Darth Vader sleeps with a Teddywookie. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]