Marco C
Marco C

Reputation: 95

PHP: Write to file, and starting where it left off

I'm very unfamiliar with PHP...

<?php
    $myfile = fopen("emaillist.txt", "w");
    $newemail = "email1\n";
    fwrite($myfile, $newemail);
    $newemail = "email2\n";
    fwrite($myfile, $newemail);
    fclose($myfile);
?>

The code is to create a database of emails. When going to emaillist.txt, the output is:

email1
email2

The output will stay like that, even if I run the php file a new line. My desired output is to for example run it twice, and get:

email1
email2
email1
email2

I'm aware storing emails in .txt files isn't smart, but this isn't for real world use.

Upvotes: 0

Views: 48

Answers (2)

bnahin
bnahin

Reputation: 816

You are using the w mode of fopen(). To place the file pointer at the end, use a or a+.

Upvotes: 1

Marco C
Marco C

Reputation: 95

It turns out I had to do fopen("emaillist.txt", "a");instead of fopen("emaillist.txt", "w");because a appends to the file, while w just writes to it.

Upvotes: 0

Related Questions