Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fwrite adds unwanted /n to text file

Status
Not open for further replies.

aspro

Programmer
Jan 22, 2003
69
AU
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";
}


I know nothing except the fact of my ignorance -Socrates
 
According to the PHP manual the file() command reads the file into an array with the newline chars still in the text.
If the next to the last line ends with a newline then the removal of the last line makes the entire text end with the newline from the next to last line.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top