Here, we’ll automate the process of adding a name to the list: addnames.php3 First, a list is chosen in lines 7-21 (this is the same function used on the index page), and then it and the email address is passed to the next script: saveemail.php3 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: newlist.php3
<html><head><title>Updating file....</title></head><body> <?$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>
blog comments powered by Disqus |