sepideh.f
sepideh.f

Reputation: 3

How to add line to the file without delete

I want to write a line to the end of the file.

My code is:

$myFile = "serialkey.txt";
$fh = fopen($myFile, 'w');
$space = "\r\n";

$stringData = "my new data";
fwrite($fh, $stringData.$space);

fclose($fh);

But when I used this code it deleted all the file and replace "my new data", I want it will not delete my file and append my data to it.

Upvotes: 0

Views: 2392

Answers (2)

ThatsNotMyName
ThatsNotMyName

Reputation: 582

You have it set to write instead of append in

$fh = fopen($myFile, 'w');

It should be

$fh = fopen($myFile, 'a');

Hope this helps.

Upvotes: 4

웃웃웃웃웃
웃웃웃웃웃

Reputation: 11984

To insert text without over-writing the end of the file, you'll have to open it for appending (a+ rather than w)

$fh = fopen($myFile, 'a+');

Upvotes: 0

Related Questions