June 13th, 2006

How to Append Data to a Text File With PHP

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;
}

Leave a reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image