fopen and r+ problem

Hi everybody,

I would like to write a simple code to add some text at the begining of
a file without erase the other data.

in my file:

news4
news3
news2
news1

if I want to add the text "news5!", the file will look like this:

news5!
news4
news3
news2
news1

So I wrote this:

<?php

if (isset($HTTP_POST_VARS['go'])) {

$content = "\r\n" . $HTTP_POST_VARS['news'] . "
";

$write = fopen('news.ini', 'r+');

fwrite($write, $content);

fclose($write);

?>


<form action="" method="post">
<input type="text" name="news" size="28" /><input type="submit"
name="go" value="Go" />
</form>

<?php

} else {

?>

<form action="" method="post">
<input type="text" name="news" size="28" /><input type="submit"
name="go" value="Go" />
</form>

<?php

}

?>

But this code doesn't work, everything is erase in my file, no new line
appears...
Could you please help me ?
Thank you, and hope my english is understandable.

--
++

Simon

--------
>> http://latox.free.fr/3d/
s1m0n [ Mi, 12 September 2007 18:24 ] [ ID #1818628 ]

Re: fopen and r+ problem

s1m0n <nomail [at] demandez.moi> wrote in news:mn.64507d79900517ae.50781
[at] demandez.moi:

> But this code doesn't work, everything is erase in my file, no new line
> appears...
> Could you please help me ?
> Thank you, and hope my english is understandable.


Open the file first for reading, put its contents into an array or other
PHP variable, then re-write the file with your new data on the first line,
followed by the original contents....
Good Man [ Mi, 12 September 2007 18:27 ] [ ID #1818629 ]

Re: fopen and r+ problem

Il se trouve que Good Man a formulé : :-[
>
> Open the file first for reading, put its contents into an array or other
> PHP variable, then re-write the file with your new data on the first line,
> followed by the original contents....

yep, good idea, thank you.

--
++

Simon

--------
>> http://latox.free.fr/3d/
s1m0n [ Mi, 12 September 2007 18:30 ] [ ID #1818630 ]

Re: fopen and r+ problem

Post removed (X-No-Archive: yes)
Notifier Deamon [ Mi, 12 September 2007 20:04 ] [ ID #1818634 ]

Re: fopen and r+ problem

s1m0n wrote:
> Hi everybody,
>
> I would like to write a simple code to add some text at the begining of
> a file without erase the other data.

Use append instead, 'a', this will add new lines to the bottom instead of at
the first position in the file (as long as you don't read ahead).




--

//Aho
Shion [ Mi, 12 September 2007 20:10 ] [ ID #1818635 ]

Re: fopen and r+ problem

J.O. Aho a couché sur son écran :
> s1m0n wrote:
>> Hi everybody,
>>
>> I would like to write a simple code to add some text at the begining of
>> a file without erase the other data.
>
> Use append instead, 'a', this will add new lines to the bottom instead of at
> the first position in the file (as long as you don't read ahead).

Thanks to both of you.

--
++

Simon

--------
>> http://latox.free.fr/3d/
s1m0n [ Fr, 14 September 2007 20:33 ] [ ID #1820642 ]
PHP » alt.php » fopen and r+ problem

Vorheriges Thema: Session cookies?
Nächstes Thema: Re: Observations regarding redirection