Deleting from a txt file

We all know how to write and read from a txt file, but I would like to learn how to create a deleting feature that allows each submission to have a delete option.

[php]<?
$gb_file = fopen( “guests.txt”, “a+” );
if ( fwrite($gb_file, “$name\t$email\n”) ) {
$body = “My message Text…\n”;
$body .= “$name\n”;
$body .= “$email\n”;
mail( “my_email@my_domain.edu”,“Guest Book Entry”, $body );
echo “Thanks!
\n”;
}
else {
echo “could not write file…
\n”;
}
?>[/php]

Does anybody know how to do this?

You’re using a text file for guestbook entries? It would be much easier with MySQL and simply run a delete query.

However to answer your question, the only way I can think of is to take all the contents, then parse it and write it back line by line, but coding it to skip the line to be deleted.

Yeah, I was thinking that it might fall along the lines of MySQL, but I just wanted to do something smaller. That’s fine though, I suppose I could do that!

Sponsor our Newsletter | Privacy Policy | Terms of Service