nikoskip
nikoskip

Reputation: 1920

Add new line to existing TXT file using SplFileObject

I have a TXT file and I neede to open it and add a new line at the end. I'm using SplFileObject:

$file = new SplFileObject( $this->fileName, 'w' );
$file->fwrite( 'my text' . PHP_EOL );

But deletes all the file content and only saves 'my text' inside.

Upvotes: 4

Views: 2329

Answers (1)

nikoskip
nikoskip

Reputation: 1920

I replaced the 'w' flag to 'a' flag. The 'w' open the file but place the pointer at the beginning, while 'a' place the pointer at the end.

Thanks to @andrewsi for this solution.

Upvotes: 4

Related Questions