After making sure the file exists, the first thing this
script does is read the current list into an array (lines 4-6), then checks for each name to see if it already exists in the list (no one likes being on the same mailing list TWICE!). It does this in lines 8-12 by writing each name back into the file, and just skipping the name if it already exists. Once every name is compared, the new name is written at the end of the file.
You’ll notice that fopen uses "w" here, for writing, while it used "r" on the index page (for reading) and "a" is used on the Send Email page(for appending to an existing file). The format for the command is:
fopen (filename, mode)
With mode being either "r", "w", or "a". Writing an existing file erases the current contents, while appending starts writing at the end of the file. Each of these modes can allow both reading and writing by placing a "+" after the mode. But the initial state (whether the current contents are deleted, and where the initial pointer is) will still occur.
Note: If you are going to open a file for reading and writing simultaneously, nasty things can happen that can destroy data in other files if a problem occurs. Good programming practice dictates you should modify a file by reading it into an array, closing the file connection (which file does automatically), making the modifications, and writing the changed contents back to the file.
Adding lists uses the same technique, Just joining the List and Filenames together with a "|". Here’s the scripts used for to add a list to your collection:
<html><head><title>Mailing List Administration</title></head><body>
<b>Both blanks must be filled in!</b><br>
<form method=post action="makenewlist.php3">
<b>Name of the list:</b><input type=text name="Listname" size="40">
<b>One word description of the list:</b>
<input type=text name="Filename" size="40">
<input type="submit" name="Submit" value="Make list">
<br><br><a href="addnames.php3">Add names to an existing list</a>.
<br><a href="picklist.php3">Edit/Delete names</a>.
<br><br><a href="data/log.txt">View Send Log</a>.
<?$Filename = $Filename.".lst";$myfile = fopen("data/lists.txt","a");fputs($myfile,$Listname."|".$Filename."\n");fclose($myfile);?>Created new list <? echo $Listname ?>.<br><br><br><a href="index.php3">Home</a>.<br><br><a href="addnames.php3">Add names to the list</a>.<br><br><a href="picklist.php3">Edit/Delete names</a>.<br><br><a href="data/log.txt">View Send Log</a>.<br></body></html>