writing to file including linefeed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cloudbase
Forum Newbie
Posts: 15
Joined: Tue Dec 13, 2011 12:11 pm

writing to file including linefeed

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: writing to file including linefeed

Post by Celauran »

Have you tried this?

Code: Select all

$newflyline = "new line\n";
cloudbase
Forum Newbie
Posts: 15
Joined: Tue Dec 13, 2011 12:11 pm

Re: writing to file including linefeed

Post by cloudbase »

Thanks!!!! :banghead:
it works exactly...."new line<br>\n";
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: writing to file including linefeed

Post by Celauran »

You shouldn't need the <br>. If you want to display the file's contents, you can call nl2br().
cloudbase
Forum Newbie
Posts: 15
Joined: Tue Dec 13, 2011 12:11 pm

Re: writing to file including linefeed

Post 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......
Post Reply