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

2001-11-19 Thread David Robley

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]




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

2001-11-19 Thread David Robley

On Tue, 20 Nov 2001 14:41, Brian Tully wrote:

> On 11/19/01 6:45 PM, "Phelps" <[EMAIL PROTECTED]> 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:[EMAIL PROTECTED]]
> >> 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:
> >>
> >>  >>
> >> $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.

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




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

2001-11-19 Thread Brian Tully

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:

 


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


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