How to Append Data to a Text File With PHP

by CarcaBot on July 24, 2009 · 0 comments

A very good practice for any e-commerce or content syndication site is to build an email subscriber list. I recently had to whip together a quick way of doing this, and didn't have the time to build a database driven system. I used PHP and a text file to build an easy subscriber system. Each new email gets appended to the text file, simple as that.

Assume we have a text file called 'subscribers.txt', then here is the code. I use a function called checkEmail (explained on this site) to verify the authenticity of the email address.

$emailaddress = $_POST['Email'];

if(checkEmail($emailaddress)) {
        $f=fopen("subscribers.txt","a"); //open text file for appending
        fwrite($f,$emailaddress . "n");
        fclose($f);
} else {
        echo(" – Invalid email<br />");
        echo("<a href=index.htm>Try again</a>");
        die;
}

Previous post:

Next post: