PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.php > fopen and r+ problem
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
fopen and r+ problem

Réponse
 
LinkBack Outils de la discussion
Vieux 12/09/2007, 17h24   #1
s1m0n
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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'] . "<br />";

$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 me ?
Thank you, and hope my english is understandable.

--
++

Simon

--------
>> http://latox.free.fr/3d/



  Réponse avec citation
Vieux 12/09/2007, 17h27   #2
Good Man
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: fopen and r+ problem

s1m0n <nomail@demandez.moi> wrote in news:mn.64507d79900517ae.50781
@demandez.moi:

> But this code doesn't work, everything is erase in my file, no new line
> appears...
> Could you please 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....

  Réponse avec citation
Vieux 12/09/2007, 17h30   #3
s1m0n
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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/



  Réponse avec citation
Vieux 12/09/2007, 19h04   #4
Tom
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: fopen and r+ problem

On Wed, 12 Sep 2007 18:24:06 +0200, 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.
>
>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'] . "<br />";
>
> $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 me ?
>Thank you, and hope my english is understandable.
>


I think if you are writing to a file, it either starts as a new file or could
set your commands to append to the end of an existing one.

If you are trying to add to the beginning, you probably need to juggle a few
files temporarily. Maybe start a new file with the content you want on top, then
append the original content to the end of that new file. You would need to
juggle file names around a bit.

Tom
--
Newsguy.com - Unlimited Accounts
Now with 32 concurrent connections

  Réponse avec citation
Vieux 12/09/2007, 19h10   #5
J.O. Aho
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 14/09/2007, 19h33   #6
s1m0n
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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/



  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h35.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10836 seconds with 14 queries