Forum Moderators: coopster

Message Too Old, No Replies

Error when editing a file with x+

Grrrrr....

         

adni18

3:39 pm on Sep 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi. Does anybody know what is wrong with this code? If I take away the fputs command and change the mode to "r" it's all fine and dandy, but I need to write to this file!

$hi = fopen("gbook.txt","x+");
$herro=fread($hi, filesize("gbook.txt"));
$he=fputs($hi, $ddd.$herro, strlen($ddd.$herro));
fclose($hi);

Please respond ASAP. :-)

mcibor

4:17 pm on Sep 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



fopen("url/file.ext", "x+") [php.net] - Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING.

So you see, if the file exists it will fail.

Depending on what you want I would use
$hi = fopen("gbook.txt","a+"); //to read and append the file

or first open to read
$hi = fopen("gbook.txt","r");

modify it and write to it:
$hi = fopen("gbook.txt","w");

Best regards
Michal Cibor

adni18

4:26 pm on Sep 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ok, so it should be

$hi = fopen("gbook.txt","r");
$herro=fread($hi, filesize("gbook.txt"));
fclose($hi);

$yo = fopen("gbook.txt","w");
$he=fputs($yo, $ddd.$herro, strlen($ddd.$herro));
fclose($yo);

?

mcibor

5:20 pm on Sep 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes! Something like this.

Best regards
Michal Cibor

jatar_k

3:44 pm on Sep 12, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



instead of 'r' you could also use 'r+' for reading and writing instead of having to call fopen twice