how do i delete a line in a file
Posted: Sun Sep 12, 2010 11:18 am
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.
Heres the code im using to read the contents of the file and display the delete button
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);?>" />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);