Jump to content

[SOLVED] \n getting removed from last line of flat file


datafan

Recommended Posts

When I use the following delete reservation script it works perfect except it removes the \n newline character from the last line of my flat file. That screws up the next line to be written to the file. Any idea? I tried to add a \n and ended up with a empy file! yikes

 

 

<?php
if ($_POST['submit']) {
$classname = $_POST['classname'];
$keyword = $_POST['keyword'];
$filename = "flatfolder/masterflat.txt";
$file_array = file($filename);

foreach ($file_array as $file_line) {
	$file_line = explode("|", trim($file_line));

	if ($file_line[4] != $classname && $file_line[6] != $keyword) {
		$new_file_array[] = implode("|", $file_line);
	}
}

$open_file = fopen($filename, 'w');
fwrite($open_file, implode("\n", $new_file_array));
fclose($open_file);
unlink("reservations/$classname");
echo "The reservation has been removed successfully.<br />";	
}
?>

 

This is what I tried but no workie.

fwrite($open_file, implode("\n", $new_file_array."\n"));

 

You almost had it, instead of

<?php
fwrite($open_file, implode("\n", $new_file_array."\n"));
?>

do

<?php
fwrite($open_file, implode("\n", $new_file_array)."\n");
?>

Notice that you have to add the "\n" to the string created by the implode().

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.