contact form with multiple file attachment

Hi,

I have just created a contact form with a file attachment field. How can I make this form with multiple file attachment?
Here is my PHP code along with the html form, all in 1 file.
[php]

function print_form(){
?>

* Required fields

Full Name *

<p><label for="emailfrom">Email <span class="required">*</span></label>
<input name="emailfrom" id="emailfrom" type="text" class="field" value="<?= $_SESSION['myForm']['emailfrom']; ?>" tabindex="2"/></p>

<p><label for="phone">Phone Number <span class="required">*</span></label>
<input name="phone" id="phone" type="text" class="field" value="<?= $_SESSION['myForm']['phone']; ?>" tabindex="3"/></p>

<p><label for="youraddress">Street Address <span class="required">*</span></label>
<input name="youraddress" id="youraddress" type="text" class="field" value="<?= $_SESSION['myForm']['youraddress']; ?>" tabindex="4"/></p>

<p><label for="yourcity">City <span class="required">*</span></label>
<input name="yourcity" id="yourcity" type="text" class="field" value="<?= $_SESSION['myForm']['yourcity']; ?>" tabindex="5"/></p>

<p><label for="states">State <span class="required">*</span></label>
<select name="states" id="states" tabindex="6">
	<option value="" selected="selected" disabled="disabled"> - Please select - </option>
	<option>ACT</option>
	<option>NSW</option>
	<option>NT</option>
	<option>QLD</option>
	<option>SA</option>
	<option>TAS</option>
	<option>VIC</option>
	<option>WA</option>
</select>

<p><label for="postcode">Postcode <span class="required">*</span></label>
<input name="postcode" id="postcode" type="text" class="field" maxlength="4" value="<?= $_SESSION['myForm']['postcode']; ?>" tabindex="7"/></p>

<p><label for="comments">Description <span class="required">*</span></label>
<textarea name="comments" id="comments" rows="7" cols="10" class="field" tabindex="8"><?= $_SESSION['myForm']['comments']; ?></textarea></p>

<p><label for="attachment">File Upload<br />(1 file only, max file size 1024kb. Allowed file formats are .jpg, .gif, .png, .pdf, .zip, .rar)</label>
<input name="attachment" id="attachment" type="file" tabindex="9">

<p><input type="checkbox" name="accept_terms" id="accept_terms" class="check" value="1" tabindex="10"> I have read and I agree to the competition terms &amp; conditions</p>

<p><input type="checkbox" name="newsletter" id="newsletter" class="check" value="Y" tabindex="11"> I agree to receiving information about new products & discounts from Bathroom Warehouse, Methven & Toto</p>

<p>Please enter the security code shown below:</p>
<p><img src="images/code.jpg" alt="code" border="0" /> <input name="spam" id="spam" type="text" value="<?= $_SESSION['myForm']['spam']; ?>" tabindex="12"></p>

<p><input type="submit" name="submit" id="submit" value="Send Email!"  tabindex="13"/></p>
<p><input type="hidden" name="submitted"  value="true" /></p>
</form>
<?php } // enquiry form validation function process_form() { // Read POST request params into global vars // FILL IN YOUR EMAIL $to = "[email protected]"; $subject = "Bathroom Competition"; $namefrom = trim($_POST['namefrom']); $emailfrom = trim($_POST['emailfrom']); $phone = trim($_POST['phone']); $youraddress = trim($_POST['youraddress']); $yourcity = trim($_POST['yourcity']); $states = trim($_POST['states']); $postcode = trim($_POST['postcode']); $comments = trim($_POST['comments']); $terms = isset($_POST['accept_terms']); $newsletter = isset($_POST['newsletter']); $spam = trim($_POST['spam']); // Allowed file types. add file extensions WITHOUT the dot. $allowtypes=array("zip", "rar", "jpg", "gif", "png", "pdf"); // Require a file to be attached: false = Do not allow attachments true = allow only 1 file to be attached $requirefile="true"; // Maximum file size for attachments in KB NOT Bytes for simplicity. MAKE SURE your php.ini can handel it, // post_max_size, upload_max_filesize, file_uploads, max_execution_time! // 2048kb = 2MB, 1024kb = 1MB, 512kb = 1/2MB etc.. $max_file_size="1024"; // Thank you message $thanksmessage="Your email has been sent, we will respond shortly."; $errors = array(); //Initialize error array //checks for a name if (empty($_POST['namefrom']) ) { $errors[]='Please enter your name'; } //checks for an email if (empty($_POST['emailfrom']) ) { $errors[]='Please enter your email'; } else { if (!eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['emailfrom'])))) { $errors[]='Please enter a valid email address'; } // if eregi } // if empty email //checks for the phone number if (empty($_POST['phone']) ) { $errors[]='Please enter your phone number'; } //checks for the street address if (empty($_POST['youraddress']) ) { $errors[]='Please enter your street address'; } //checks for the city if (empty($_POST['yourcity']) ) { $errors[]='Please enter your city'; } //checks for the state if (empty($_POST['states']) ) { $errors[]='Please select a state'; } //checks for the postcode if (empty($_POST['postcode']) ) { $errors[]='Please enter your postcode'; } //checks for a message if (empty($_POST['comments']) ) { $errors[]='Please enter a description'; } /* Tick the checkbox to validate the form */ if ($terms != 1) { $errors[]='You must agree to the terms & conditions before submitting the form'; } /* If security code is not valid show error message */ if (empty($_POST['spam']) ) { $errors[]='Please enter the code shown on the image'; } else { if ($spam!="93461") { $errors[]='Security code is not valid'; } // if $spam } // if empty spam // checks for required file // http://amiworks.co.in/talk/handling-file-uploads-in-php/ if($requirefile=="true") { if($_FILES['attachment']['error']==4) { $errors[]='You forgot to attach a file'; } } //checks attachment file // checks that we have a file if((!empty($_FILES["attachment"])) && ($_FILES['attachment']['error'] == 0)) { // basename -- Returns filename component of path $filename = basename($_FILES['attachment']['name']); $ext = substr($filename, strrpos($filename, '.') + 1); $filesize=$_FILES['attachment']['size']; $max_bytes=$max_file_size*1024; //Check if the file type uploaded is a valid file type. if (!in_array($ext, $allowtypes)) { $errors[]="Invalid extension for your file: ".$filename.""; // check the size of each file } elseif($filesize > $max_bytes) { $errors[]= "Your file: ".$filename." is to big. Max file size is ".$max_file_size."kb."; } } // if !empty FILES if (empty($errors)) { //If everything is OK // send an email // Obtain file upload vars $fileatt = $_FILES['attachment']['tmp_name']; $fileatt_type = $_FILES['attachment']['type']; $fileatt_name = $_FILES['attachment']['name']; // Headers $headers = "From: $emailfrom"; // create a boundary string. It must be unique $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // Add the headers for a file attachment $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; // Add a multipart boundary above the plain message $message ="This is a multi-part message in MIME format.\n\n"; $message.="--{$mime_boundary}\n"; $message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n"; $message.="Content-Transfer-Encoding: 7bit\n\n"; $message.="From: ".$namefrom."\n"; $message.="Email: ".$emailfrom."\n"; $message.="Phone: ".$phone."\n"; $message.="Street address: ".$youraddress."\n"; $message.="City: ".$yourcity."\n"; $message.="State: ".$states."\n"; $message.="Postcode: ".$postcode."\n"; $message.="Newsletter: ".$newsletter."\n"; $message.="Description: ".$comments."\n\n"; if (is_uploaded_file($fileatt)) { // Read the file to be attached ('rb' = read binary) $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); // Base64 encode the file data $data = chunk_split(base64_encode($data)); // Add file attachment to the message $message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; } // Send the completed message $envs = array("HTTP_USER_AGENT", "REMOTE_ADDR", "REMOTE_HOST"); foreach ($envs as $env) $message .= "$env: $_SERVER[$env]\n"; if(!mail($to,$subject,$message,$headers)) { exit("Mail could not be sent. Sorry! An error has occurred, please report this to the website administrator.\n"); } else { echo '

Thank You!

'. $thanksmessage .'

'; unset($_SESSION['myForm']); print_form(); } // end of if !mail } else { //report the errors echo '

Error!

The following error(s) has occurred:
'; foreach ($errors as $msg) { //prints each error echo " - $msg
\n"; } // end of foreach echo '

Please try again

'; print_form(); } //end of if(empty($errors)) } // end of process_form() ?>

[/php]

Thank you for any help.

Using more <input type=‘file’.

Is this your question ?

yes it is my question. Would the php code stay the same even if I add more attachments?
Thanks

With the same syntax

$_FILES[‘attachment’] contains array of file ‘atachment’
$_FILES[‘attachment1’] contains array of file ‘atachment1’
$_FILES[‘attachment2’] contains array of file ‘atachment2’,

It’s same of multiple var in post form…

$_POST[X] contains X
$_POST[‘Y’] contains Y

You will do that you want with this

Sponsor our Newsletter | Privacy Policy | Terms of Service