?
Solved

fwrite adds extra carriage return to last line of file

Posted on 2005-04-19
8
Medium Priority
?
1,113 Views
Last Modified: 2008-01-09
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";
}

0
Comment
Question by:bluefishsw
8 Comments
 
LVL 10

Expert Comment

by:bugada
ID: 13822259
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
 
LVL 5

Accepted Solution

by:
dougday earned 2000 total points
ID: 13824220
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
 
LVL 5

Expert Comment

by:dougday
ID: 13824250
Ah, and I forgot to put an fclose($handle); after the fwrite($handle, $data); line.
-Doug

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Expert Comment

by:_GeG_
ID: 13824626
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
 
LVL 9

Expert Comment

by:_GeG_
ID: 13824637
ok, found a typo
    file_write_contents($filename, preg_replace("|\\n?[^\\n]*$removefile[^\\n]*\\n?|", "\n", file_get_contents($filename)));
0
 

Author Comment

by:bluefishsw
ID: 13829809
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
 
LVL 5

Expert Comment

by:dougday
ID: 13829949
No problem.  Glad it helped
-Doug
0
 
LVL 5

Expert Comment

by:dougday
ID: 13830239
Thanks for the A :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month16 days, 3 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question