fwrite adds extra carriage return to last line of file

This problem has me baffled.
I have a form which outputs the lines of a txt file into a table creating links. for each link there is a radio button which sends exact line form the txt file to the action page when the button is pressed.

In the action page all I want to do is put all of the lines in an array, do a string compare to find the line to remove (which comes from the form) then rewrite all of the lines minus the one to remove, back to the text file.
this works fine if you take out a line at the start or the middle but as soon as you try to remove the last line it rewrites the txt file and adds a carriage return at the end. For no apparent reason. It does not get added in my code.
here is my action page.

Please help...

$removefile = $_REQUEST['file'];
$filename = "../newsletters.txt";
$arrFp = file($filename);
$numLines = count($arrFp);
if (is_writable($filename)){
   if (!$handle = fopen($filename, "w" )) {
    exit;
   }
      for($i=0; $i<$numLines; $i++)
      {
         if(!strstr($arrFp[$i],$removefile)){
          if (filesize($textfile)>0){    
          $inputappend = "\n".$arrFp[$i];
          fwrite($handle, $inputappend);
           }
               else{
          fwrite($handle, $arrFp[$i]);
           }        
      }
    }
    list($file, $linkname) = split(";", $removefile);    
    $file = "../uploads/".$file;
    unlink( $file );
    echo "<h3>Your file has been removed successfully</h3>
}
else {
echo "Problem opening file $textfile";
}

bluefishswAsked:
Who is Participating?
 
dougdayCommented:
Why not do this instead?

$removefile = $_REQUEST['file'];
$filename = "../newsletters.txt";
$arrFp = file($filename);
$numLines = count($arrFp);
$line_arr = array();

for($i=0; $i<$numLines; $i++) {
    if(!strstr($arrFp[$i],$removefile)) {
        $line_arr[] = $arrFp[$i];
    }
}

if (is_writable($filename)) {
    if (!$handle = fopen($filename, "w" )) {
        exit;
    }

    $data = implode("\n", $line_arr);
    fwrite($handle, $data);

    list($file, $linkname) = split(";", $removefile);    
    $file = "../uploads/".$file;
    unlink( $file );
    echo "<h3>Your file has been removed successfully</h3>
}
else {
    echo "Problem opening file $textfile";
}

-Doug
0
 
bugadaCommented:
Well, in yuor code you check the filesize of a $textfile file.
Probably you have to do this on $filename file.

You may speedup this check using a boolean variable (i.e. $first) initialized to true, that will be set to false after the fwrite($handle, $arrFp[$i]);


0
 
dougdayCommented:
Ah, and I forgot to put an fclose($handle); after the fwrite($handle, $data); line.
-Doug

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
_GeG_Commented:
grin, and for the very lazy php5 user:

<?php
$removefile = $_REQUEST['file'];
$filename = "../newsletters.txt";
if (is_writable($filename)){
    file_write_contents($filename, preg_replace("|\\n?[^\\n]*$removefile[^\\n]\\n?|", "\n", file_get_contents($filename)));
    list($file, $linkname) = split(";", $removefile);    
    $file = "../uploads/".$file;
    unlink( $file );
    echo "<h3>Your file has been removed successfully</h3>
}
else {
    echo "Problem opening file $textfile";
}
?>

haven't tested the regexp
0
 
_GeG_Commented:
ok, found a typo
    file_write_contents($filename, preg_replace("|\\n?[^\\n]*$removefile[^\\n]*\\n?|", "\n", file_get_contents($filename)));
0
 
bluefishswAuthor Commented:
thanks dougday. it was pretty close to what i needed. my main problem was that I has "/n"s in my array to start with so adding the implode /n just doubled my problem. i solved it by trimming the array contents.

<?php
$removefile = $_REQUEST['file'];
$filename = "../newsletters.txt";
$arrFp = file($filename);
$numLines = count($arrFp);
$line_arr = array();


for($i=0; $i<$numLines; $i++) {
      if(!strstr($arrFp[$i],$removefile)) {
            echo "This is what is goin in $arrFp[$i]";
            $line_arr[] = trim($arrFp[$i]);
      }
}
if (is_writable($filename)) {
      if (!$handle = fopen($filename, "w" )) {
             exit;
      }

      $data = implode("\n",$line_arr);
      fwrite($handle, $data);
      fclose($handle);
      
      list($file, $linkname) = split(";", $removefile);    
          $file = "../uploads/".$file;
          unlink( $file );
          echo "<h3>Your file has been removed successfully</h3>";
}
else {
    echo "Problem opening file $textfile";
}

?>
0
 
dougdayCommented:
No problem.  Glad it helped
-Doug
0
 
dougdayCommented:
Thanks for the A :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.