Page 1 of 1

writing to file including linefeed

Posted: Tue Dec 13, 2011 12:38 pm
by cloudbase
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:

Code: Select all

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:
[syntax=php]
$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);
Running the script 4 time this is what I get:
new linenew linenew linenew line

What I want should look like this:
new line
new line

Re: writing to file including linefeed

Posted: Tue Dec 13, 2011 1:24 pm
by Celauran
Have you tried this?

Code: Select all

$newflyline = "new line\n";

Re: writing to file including linefeed

Posted: Tue Dec 13, 2011 2:36 pm
by cloudbase
Thanks!!!! :banghead:
it works exactly...."new line<br>\n";

Re: writing to file including linefeed

Posted: Tue Dec 13, 2011 3:13 pm
by Celauran
You shouldn't need the <br>. If you want to display the file's contents, you can call nl2br().

Re: writing to file including linefeed

Posted: Wed Dec 14, 2011 10:48 am
by cloudbase
Hi again ALL - Thanks a bunch you guys ROCK.
Actually I found this little thing called PHP_EOL that puts a eol "thing" recognizable by php's fgets() :crazy:
**I'm not a programmer, everything I know I learned in the last 3 weeks from php.net. Would have helped if somewhere in the documentation for fgets() there was mention of PHP_EOL.
But anyway -THANKS! you here are a great help......