Jump to content


Photo

write to txt file - but not erase last entry


  • Please log in to reply
5 replies to this topic

#1 GroundHogDay

GroundHogDay
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 04 October 2006 - 07:25 PM

Hi,

I'm working on a script to pass time stamped IP addy's to a txt file, simple enough...

However, with each new entry I don't want the previous entries to be erased.

How would I go about doing this? Alternatively could you point me to a tutorial, thanks :)

The other option would be write each entry to a db, but, as I've no real experience with this, I'm taking the softer option.

#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 04 October 2006 - 07:38 PM

You could do something like this.

//your file
$file = 'path/to/file';
//whatever you are going to write to the file
$content = 'whatever';
//now we open the file
$fp = fopen($fp, $file, 'r+');
//first we read the contents of the file into a variable
$rest = fread($fp, filesize($fp));
//now we write the new content to the file
fwrite($fp, $content);
//now we rewrite the old content back.
fwrite($fp, $rest);
//now close the file
fclose($fp);

EDIT I forgot a , in the $fp..

Hope that helps,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 GroundHogDay

GroundHogDay
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 04 October 2006 - 08:57 PM

Thanks Tom :) that should work a treat!

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 04 October 2006 - 09:00 PM

why not just append to the end of the file? use fopen() with 'a' instead of 'r+', and the pointer will be at the end of the file already, and you can simply fwrite() to it without having to worry about overwriting anything.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 04 October 2006 - 09:00 PM

Why not just open the file for append?
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 05 October 2006 - 02:54 PM

Why not just open the file for append?

sounds a lot like what i suggested :P hehe
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users