Check for duplicate names in text file

I’ve got a form that submits email addresses to a text file.
I need the same script that appends these emails to this text file, to check for duplicate emails in the text file and do something if there is a duplicate.

Here’s the code I have now:


$filename = "emails.txt";

$fd = fopen ($filename, "a") or die ("Can't open $filename");
$fname = $email;
$fout = fwrite ($fd, "$fname
");
fclose($fd);

header("Location: index.php?msg=success");
exit();

So i’ve already got the emails adding to the text file separate by new lines. I just need some type of if/else to check if an email is already in there, and if not, add the email. I started reading up on explode but it’s the end of the day and I’m fried :wink:

Thanks.