fopen problems. Can't write to first line with simple code.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
seg
Forum Commoner
Posts: 38
Joined: Thu Oct 31, 2002 12:08 pm
Location: Northern, VA
Contact:

fopen problems. Can't write to first line with simple code.

Post by seg »

I have been using a long script to overcome a problem I have been having. Here is the long version.

Code: Select all

// get old
$fp = fopen("$filename","r");
$old = fread($fp, filesize ($filename));
fclose($fp);
// kill file, write new then old
$fp = fopen($filename,"w");
fputs($fp,"$new $old");
fclose($fp);
This will save all the current data, and write the new info to the first line, and everything under it.

Seems the simple way to do this would be

Code: Select all

$fp = fopen($filename, "r+");
fputs($fp, $new);
fclose($fp);
But that does not work. PHP.net says the "r+" tag keeps the data, and puts the fp at the beginning of the file. In practice, though, it will post the new info once, then from that point on, it will post only part of the $new variable, or cut existing data. I've got no friggin idea what is going on with this. Can someone shed some light? php.net says this should work.
thanks
-seg
seg
Forum Commoner
Posts: 38
Joined: Thu Oct 31, 2002 12:08 pm
Location: Northern, VA
Contact:

Post by seg »

I just re-read my post, wow, I was dead tired last night. I'm going to do some actual testing and figure this out today. Reading my post now I not surprised no one responded. heh. If I still can't figure this crap out, hopefully I'll be able to compile a direct question with a more accurate description of the problem.
Post Reply