views:

23

answers:

1

I'm building a site in Zend that requires the ability to upload a list of email addresses via a file upload. This will end up populating a distribution list table.

What I'd like to do is have the user complete the form, including the attached file, then upon submit, I will parse the file and submit the form for each line of the file.

Ex. User completes the form with the distribution list name, and uploads a file ( properly formatted)

On submit, I need to parse the file for each email address, then add a row of data to the table with the distribution list name and a new address.

Any ideas, I'm open to all suggestions.

Thanks for the help, you guys are awesome

A: 

Assuming that you are actually getting the file onto the server already (as described in the manual), then you're not really dealing with a Zend, or even an upload, issue, any more. You're simply dealing with a "read and parse data in a file" issue.

Start by looking at PHP's filesystem documentation

Assuming your files aren't huge, and you've got one email address per line, you might do something like:

<?PHP
$data = file_get_contents('/path/to/your/uploaded/file');
$data = explode("\n",$data);
foreach($data as $address){
   //validate each address, and if valid, safely insert into your database
}

if you've got many thousands of email addresses in the file, you might want to avoid loading them all into memory at once. In that case, use fopen, and loop/fgets each line.

If you need to parse CSV data, fgetcsv will save you many headaches.

See the code examples at the above-linked PHP manual pages for more guidance on looping over file data.

timdev
Thanks Tim,I never even thought of it from that angle.
Jake Middleton