ali_groves Posted September 4, 2007 Share Posted September 4, 2007 First off, I'm new to the PHP Freak Forums, so hello to everyone. Second off, I'm relatively new to php (about 5 weeks in) so please be gentle with me! :-\ Ok, down to business! I am trying to design a form which can be used for a newsletter, sending an email with an attachment, using phpmailer. The form does work(!) as in, it sends the email and attachment(s) to the email addresses gathered from the database, however there are 3 things I need help with, surrounding the attachment validation. (1) --- The scary error message: The script includes a function "checkSize" which checks the attachment file is not too big. However on testing I have found that on attaching an oversized file, the following error message is displayed... Warning: Variable passed to each() is not an array or object in C:\wamp\www\dbtoots\thrucare2\phpdesigner_tmp110.php on line 34 Warning: Variable passed to each() is not an array or object in C:\wamp\www\db toots\thrucare2\phpdesigner_tmp110.php on line 64 Sorry the server was unable to upload the files..." --------------- Note - Line 34 is: while(list($key,$value) = each($_FILES[images])) Note - Line 64 is: while(list($key,$value) = each($_FILES[images][name])) ---------------- So although this part works (sort of!) by not uploading the files, I'm guessing its not working properly, and I need help deciphering why. I grabbed the attachment functions from a script on the web, and im not 100% sure on what the lines surrounding the globals and the while(list($key,$value) = each($_FILES[images])) are actually doing, so if someone could help explain that, that would be great too. (2) --- Moving the function Ultimately, once (1) is fixed, I'd like to move function checkSize into the function validate form, so that it can be added to the $error array, and if there is an error it can be shown using the print_error('file', $errors) part on the form which i have set up in anticipation. This move might be easy once (1) is fixed, so I might not need help, but any pointers would be greatly received. (3) --- Repeating the form details If the validate form function finds errors, it reshows the form, with error messages, and previously entered data is also re-shown ($defaults). I'd like to extend this to include the paths to the attachments if appropriate, but i have no idea on this one at present. (3a) --- Paranoid Android (i know i said there were only 3 things...) This one might not be neccessary, I'm not sure. As checkSize displays errors which include line 64 - part of the function upload file - I am concerned that perhaps the file is not being temporarily uploaded to the upload file, but is being sent from the local computer each time (which may use more bandwidth?). Not entirely sure I know what I mean with this, hoping someone can shed some light, and let me know if I need to worry. Thanks to anyone who read this far, and double thanks to anyone who can shed some light and offer small glimmers of hope. Ali <?php ($page = newsletter); include_once("header.php"); if ($_SERVER['REQUEST_METHOD'] == 'GET') { // Just display the form if the request is a GET display_form(array()); } else { // The request is a POST, so validate the form $errors = validate_form(); if (count($errors)) { // If there were errors, redisplay the form with the errors display_form($errors); } else { // The form data was valid, so send the email and tell the user all is well require("class.phpmailer.php"); $dir = "uploads/$filename"; $attachments = array(); checkSize(); } } //---CheckSizeFunction ---\\ function checkSize(){ global $result, $MV ,$errors,$BackLink; while(list($key,$value) = each($_FILES[images][size])) { $maxSize = 5000000; if(!empty($value)){ if ($value > $maxSize) { echo"Sorry this is a very big file .. max file size is $maxSize Bytes = 5 MB"; exit(); } else { $result = "File size is ok <br>"; } } } uploadFile(); } //-------END OF Check Size--------\\ //==============upload File Function============\\ function uploadFile() { global $attachments; while(list($key,$value) = each($_FILES[images][name])) { if(!empty($value)) { $filename = $value; array_push($attachments, $filename); $dir = "uploads/$filename"; //chmod("uploads",0777); $success = copy($_FILES[images][tmp_name][$key], $dir); } } if ($success) { echo " Files Uploaded Successfully<BR>"; SendIt(); }else { exit("Sorry the server was unable to upload the files..."); } } //--------------------------- send it function ----------------------------// function SendIt() { global $attachments,$name,$Email_to,$Email_msg,$email_subject,$email_from; $mail = new PHPMailer(); $mail->IsSMTP(); // telling the class to use SMTP $mail->Host = "home.example.com"; // SMTP server $mail->From = "jdoe@example.com"; // from email $mail->FromName = "John Doe"; //from name $mail->Subject = stripslashes($_POST['subject']); // subject line $mail->Body = stripslashes($_POST['message']); //message $mail->WordWrap = 50; foreach($attachments as $key => $value) { //loop the Attachments to be added ... $mail->AddAttachment("uploads"."/".$value); } include_once("database_conn.php"); $sql = "SELECT newsemail FROM newsletter"; $res = @mysql_query($sql) or die("Couldn't get addresses."); // loop through the result set and send mail while ($email_row = @mysql_fetch_array($res)) { // get the recipient address $to = $email_row['newsemail']; $mail->AddAddress("$to"); //recipent email and name if(!$mail->Send()){ echo "There has been a mail error sending to " . $to . "<br>";} else { echo "The newsletter was successfully sent to " . $to . "<br>";} // Clear all addresses and attachments for next loop $mail->ClearAddresses(); } // after mail is sent with attachments , delete the images on server ... foreach($attachments as $key => $value) {//remove the uploaded files .. unlink("uploads"."/".$value); } } function display_form($errors) { // Set up defaults and the filled in form parts to be returned, with any html characters changed to entities $defaults['subject'] = isset($_POST['subject']) ? htmlentities($_POST['subject']) : ''; $defaults['message'] = isset($_POST['message']) ? htmlentities($_POST['message']) : ''; ?> <div id="main"> <form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' method='post' enctype='multipart/form-data'> <dl> <dt><label for="subject">Your subject:</label></dt> <?php print_error('subject', $errors) ?> <dd><input type="text" name="subject" size="50" value="<?php echo $defaults['subject'] ?>"/></dd> <dt><label for="email">Your message:</label></dt> <?php print_error('message', $errors) ?> <dd><textarea name="message" cols="50" rows="15"><?php echo $defaults['message'] ?></textarea></dd> <?php $max_no_img=2; // Maximum number of files value to be set here for($i=1; $i<=$max_no_img; $i++){ echo "<dt><label for='File $i'>File $i:</label></dt>" . print_error('file', $errors) . "<dd><input type=file name='images[]'><br /></dd>"; } ?> </dl> <input type='submit' value='Send Info'/> </form> </div> <?php } // A helper function to make generating the HTML for an error message easier function print_error($key, $errors) { if (isset($errors[$key])) { print "<dd class='error'>{$errors[$key]}</dd>"; } } function validate_form() { // Start out with no errors $errors = array(); if (isset($_POST['subject']) && (empty($_POST['subject']))) { $errors['subject'] = 'Please enter a subject'; } if (isset($_POST['message']) && (empty($_POST['message']))) { $errors['message'] = 'Please enter a message'; } return $errors; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67886-newsletter-form-attachment-validation-help-greatly-received/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.