Log in

View Full Version : writing to file including linefeed



cloudbase
12-13-2011, 05:02 PM
Good morning everyone. I just started in php so please forgive my ignorance.
I'm trying to append $newflyline to a $flyline that contains $oldflyline then echo fgets($file). My problem is fgets is reading to the eof not the expected eol. Guess I'm missing the <lf> thing?
Here's what I have:


$flyline = 'testscript.txt';
$oldflyline = file_get_contents($flyline);
$newflyline = "new line<lf>";
//- - - open - write - close flyline - - - - - - - - - - - - - - - - - - - - -
$flockfile = fopen($flyline, 'w+') or exit('Unable to open file!');
if (flock($flockfile, LOCK_EX)) {
fwrite($flockfile, $newflyline);
fwrite($flockfile, $oldflyline);
flock($flockfile, LOCK_UN);}
else echo "oops! Try again, no lock on file";
fclose($flockfile);
//- - - table display - - - - - - - - - -
$file = fopen($flyline,'r') or exit('Unable to open file!');
$i="1";
while ($i <= 2){
echo fgets($file); $i++;
}fclose($file);


This is what I'm getting running it 4 times;
new linenew linenew linenew line
what I want is to control the number of lines displayed with while($i<=2)
new line
new line

cloudbase
12-13-2011, 08:39 PM
"new line<br>\n";
was the answer - got it now :confused: