Hello everyone!

I'm have a text file's contents, its output in the browser and a php file (which I'm including below.) What I need to do is search for a specific line in the text file and delete it. The problem is that the end result will have a text file with user's comments and a page will be generated that will show all comments, a check box for marking any particular comment for deletion and when the submit box will be pressed, the entry will be deleted (the entire line of the file.)

Here's what I have so far...

php file to read the comments.txt

<?
// get comments file
$filename = 'comments.txt';
$fp = fopen( $filename, 'r' );
$file_contents = @fread( $fp, filesize( $filename ) );
fclose( $fp );

// print comments file's file name (not going to be used in the end product)
print ("filename<br>");
print $filename;
print ("<br><hr><br>");
// demo to show how the file is visible
print ("show file<br>");
include 'comments.txt';

// the problem comes when you look at the actual file and its formatting compared to the output
?>

The comments.txt file itself (it's ONLY for testing, not the actual end result file)

1|<b>this is a test</b>|<br><i>12.31.07 1:45 pm</i>|<br>comment #1<br><br>
1|<b>this is another test</b>|<br><i>12.31.07 2:14 pm</i>|<br>comment #2<br><br>
1|<b>this is yet another test</b>|<br><i>12.31.07 2:14 pm</i>|<br>comment #3<br><br>
1|<b>this is and yet another test</b>|<br><i>12.31.07 1:45 pm</i>|<br>comment #4<br><br>
1|<b>this is still another test</b>|<br><i>12.31.07 2:14 pm</i>|<br>comment #5<br><br>
1|<b>this is the last test</b>|<br><i>12.31.07 2:14 pm</i>|<br>comment #6<br><br>

The output on the browser
filename
comments.txt

--------------------------------------------------------------------------------

show file
1|this is a test|
12.31.07 1:45 pm|
comment #1

1|this is another test|
12.31.07 2:14 pm|
comment #2

1|this is yet another test|
12.31.07 2:14 pm|
comment #3

1|this is and yet another test|
12.31.07 1:45 pm|
comment #4

1|this is still another test|
12.31.07 2:14 pm|
comment #5

1|this is the last test|
12.31.07 2:14 pm|
comment #6

The output is in the following format:

1|this is a test| ( and ones following )
Date time|
comment #x ( actual comments )

I figured out how to separate the actual file into the component parts with the following script:

<?
$filename = 'comments.txt';
$fp = fopen( $filename, 'r' );
$file_contents = @fread( $fp, filesize( $filename ) );
fclose( $fp );
$lines = explode("\n", $file_contents);
$numlines = count($lines);
for ($i = 0; $i < $numlines; $i++) { 
$comment = explode("|", $lines[$i]);
print "This is the comment title:";
print $comment[1];
print "<br>";
print "This is the comment date - time:";
print $comment[2];
print "<br>";
print "This is the comment text:";
print $comment[3];
print "<br>";
}
?>

As it is unknown what the actual comments (date/time/actual comment) will be, how do I search & delete something that I do not know what the contents are? I guess what I need to do now is to search for the matching article, delete the entire line & then rewrite the file. I can search (as noted in above code snippets); however, how do I take that info, find the appropriate line, delete the entire line and then rewrite the file?? Where do I go from here??? Any help would be GREATLY appreciated. Thank you in advance!

Hi wrstrong,

Looks like someone else is having the same problem you are... This is the thread that was produced for review:

http://www.webmasterworld.com/php/3304270.htm

And here is the final result PHP that was used to manipulate the flat file database:

<?php

$selLine = $_GET['id'];
$delType = $_GET['type'];
$file = "store/" . $delType . ".txt";
$news = file($file);
$cnt = 0;

foreach ($news as $key => $line) {
if ($key!= $selLine) { $result[] = $line; }
$cnt+1;
}
if ($key!= 0) {
$fh = fopen($file, "w");
foreach ($result as $nyhet) {
fwrite($fh, $nyhet);
}
fclose($fh);
}
elseif ($key == 0) {
$fh = fopen($file, "w");
fwrite($fh, "");
fclose($fh);
}

?>

Nice solution as far as I can tell, hope it helps you out. :)

Someone had asked the same question here.

Thanks for the quick reply! Although it might seem like it's that simple, I must be really dense tonight (or overly tired). LOL Here's where I am so far...

As I already know how to put the text into the comments.txt, I'm not going to include it here.

comments.txt

Here is the first title!~12.31.2007~2:14 pm~Here is the first entry!
Here is the second title!~12.31.2007~2:14 pm~Here is the second entry!
Here is the third title!~12.31.2007~2:14 pm~Here is the third entry!
Here is the fourth title!~12.31.2007~2:14 pm~Here is the fourth entry!
Here is the fifth title!~12.31.2007~2:14 pm~Here is the fifth entry!
Here is the sixth title!~12.31.2007~2:14 pm~Here is the sixth entry!
Here is the seventh title!~12.31.2007~2:14 pm~Here is the seventh entry!
Here is the eighth title!~12.31.2007~2:14 pm~Here is the eighth entry!
Here is the ninth title!~12.31.2007~2:14 pm~Here is the nineth entry!
Here is the tenth title!~12.31.2007~2:14 pm~Here is the tenth entry!

readtest.php (simply reads the file, displays it & provides a link for deletion)

<?
print ("<table border=0>");
$filename = 'comments.txt';
$fp = fopen( $filename, 'r' );
$file_contents = @fread( $fp, filesize( $filename ) );
fclose( $fp );
$lines = explode("\n", $file_contents);
$numlines = count($lines);
for ($i = 0; $i < $numlines-1; $i++) { 
$comment = explode("~", $lines[$i]);
$new_entry[$i] = $comment[0] . "<br>" . $comment[1] . "&nbsp;@&nbsp;" . $comment[2] . "<br>" .  $comment[3] . "<br>";
print ("<tr>
		  <td width=60%>$new_entry[$i]</td>
		  <td>
		   <p align=center><a href=delete_entry.php?entry=");
print $i+1;
print (">delete</a></td>
	    </tr>
	    <tr>
		  <td colspan=2><hr size=1></td>
	   </tr>");
}
print ("</table>");
?>

delete_entry.php (When used as delete_entry.php?entry=3, for example, it should delete the third entry.)

<?
$filename = 'comments.txt';
$fp = fopen( $filename, 'r+' );
$file_contents = @fread( $fp, filesize( $filename ) );

$lines = explode("\n", $file_contents);
$numlines = count($lines);
for ($i = 0; $i < $numlines-1; $i++) 
{
 $comment = explode("~", $lines[$i]);
 if (!($comment[$i] == $entry))
  {
   $together .= $comment[$i];
   if (!($i = $numlines))
    {
     $together .= "~";
    }
  }
 else
  {
  }
}
fwrite ( $filename , $together );
fclose( $fp );
?>

So far, the best that I can get as far as output is...

Warning: fwrite(): supplied argument is not a valid stream resource in {path}\delete_entry.php on line 23

{path} is the actual path on the server (not important).

Have I missed something? I need to use a flat file instead of a mysql database or a microsoft office mdb file because it needs to be quickly editable by ANY system at will and the EASIEST solution in that case was to make sure that everyone who needs to edit it will have the tools that they need WITHOUT installing anything or knowing any type of, 'special something' in order to do what they need to do.

I am usually a quick learner; however, I've searched & read everything I can on the subject and I'm stuck at this point.

Thank you for all your help thus far!

fwrite ( $filename , $together );

You write using a file pointer and not directly to a file.

$filename = 'comments.txt';
$fp = fopen( $filename, 'w+' );
fwrite($fp,$together);
fclose($fp);

What's the difference between::

$filename = 'comments.txt';
$fp = fopen( $filename, 'r+' );

:::: Then stuff in between ::::

fwrite ( $filename , $together );
fclose( $fp );

and

$filename = 'comments.txt';
$fp = fopen( $filename, 'w+' );
fwrite($fp,$together);
fclose($fp);

Other than the obvious that there is space in the one where it's not in the other and that the r+ says read and write from the beginning of the file & the w+ says read & write from the erasing of the entire file? Why are the differences so critical??

In your case, you are trying to write the contents of $together directly to a file which wouldn't work. In my case, I am writing it to the file using the file pointer $fp. Using modes r+, w+ depends on how you want to handle the file.

I decided to use a database instead. I had it to the point where I was able to see & write to the file; however, I was unable to edit or delete lines from the text file.

I have just combined all codes into one php file. Here it is -

<?php
// Assumed master file name "index.php" and text file name "comments.txt".
error_reporting(1);         // Stop error reporting for notices about undefined variables

// > Delete icon file location. Choose from -
//  http://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/DeleteRed.png
//  http://icons.iconarchive.com/icons/custom-icon-design/pretty-office-5/256/Folder-Delete-icon.png
//  http://png-1.findicons.com/files//icons/1374/world_of_aqua_candybar/128/the_delete_icon.png
//  http://4.bp.blogspot.com/_SYFwD3Jn-PE/SwMHijXgR1I/AAAAAAAAABE/BU6JbHne3Vk/s1600/undeletable+file.jpg
$image = "http://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/DeleteRed.png";

$image_width = 16;          // Icon image Width
$image_height = 16;         // Icon image Height

$loc = "index.php";         // master file [this file] location
$file = "comments.txt";         // Text file location

$news = file($file);
echo "<table border=\"1\">\n<tr><td>Line No.</td><td>Text</td><td>Del</td></tr>";
foreach ($news as $key => $value) {
    $key = $key+1;
    echo "<tr><td>Line: $key</td><td>".$value."</td><td><a href=\"?fpath=".$file."&lid=".$key."\"><img border=\"0\" src=\"".$image."\" alt=\"Delete Line\" width=\"".$image_width."\" height=\"".$image_height."\" /></a></td></tr>";
}
echo "</table>\n";

################### Delete Codes Start #############################################
if ($_GET['fpath'] && $_GET['lid']) {
    $filed = $_GET['fpath'];
    $dline = $_GET['lid'];

    $newsd = file($filed); 
    $cnt = 0;

    foreach ($newsd as $key => $line) { 
        $key = $key+1;
        if ($key != $dline) { $result[] = $line; } 
        $cnt+1; 
    } 
    if ($key!= 0) { 
        $fh = fopen($filed, "w"); 
        foreach ($result as $nyhet) { 
            fwrite($fh, $nyhet); 
        } 
        fclose($fh); 
        header ("Location: $loc");
    } 
    elseif ($key == 0) { 
        $fh = fopen($filed, "w"); 
        fwrite($fh, "");
        fclose($fh); 
        echo "<br /><br /><br /><br /><center><h3>No lines have been deleted! You must enter a valid \"<b>Line ID</b>\"!</h3></center>";
    }
}
################### Delete Codes End ###############################################

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.