how do i delete a line in a file

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
jimmyd1987
Forum Newbie
Posts: 1
Joined: Sun Sep 12, 2010 10:47 am

how do i delete a line in a file

Post by jimmyd1987 »

I've created a comments page which reads and writes to a comments.txt file. ive made it so that with every comment printed a button appears to delete that comment.How do i make the delete button delete that same line of text?. I have this code that can delete a defined number of lines in a text file. What do i need to do to make it delete the same line of text.

Code: Select all

<?php
function delxlines($txtfile, $numlines)
{
if (file_exists("temp.txt"))
{
unlink("temp.txt");
}

$arrayz = file("$txtfile");
$tempWrite = fopen("temp.txt", "w");
for ($i = $numlines; $i < count($arrayz); $i++)
{
fputs($tempWrite, "$arrayz[$i]");
}
fclose($tempWrite);
copy("temp.txt", "$txtfile");
unlink("temp.txt");
}
//delxlines("comments.txt", 3);
?>
<input type="submit" value="" onsubmit="<?php delxlines("comments.txt", 3);?>" />
Heres the code im using to read the contents of the file and display the delete button

Code: Select all

$file = fopen("comments.txt", "r") or exit("Unable to open file!");

//Output a line of the file until the end is reached
while(!feof($file))
  { 
echo fgets($file);
echo "<input type=\"submit\" value=\"Delete\" onsubmit=\"<?php del_line_in_file('comments.txt', 'wait');?>\" /><br />";
echo "<br />";
}
fclose($file);
User avatar
angelicodin
Forum Commoner
Posts: 81
Joined: Fri Nov 13, 2009 3:17 am
Location: Oregon, USA

Re: how do i delete a line in a file

Post by angelicodin »

can I ask why not use a sql/mysql/other database, to handle all of this? would be vary easy to do with that.
Post Reply