azukah Posted March 23, 2010 Share Posted March 23, 2010 hi- I have this form that works fine: if an image is not the right format or it's too big, it tells the user the error msg and redirects them back to the form BUT it still sends the email... how do i go about sending the email ONLY if the image is the right format & it's within the size limit..??? <?php define ("MAX_SIZE","100"); function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $errors=0; if(isset($_POST['Submit'])) { $image=$_FILES['image']['name']; if ($image) { $filename = stripslashes($_FILES['image']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { $error = "<p>Sorry... only jpg, jpeg, gif and png formats are accepted!</p><p><a href='postRequest.php'>Please try again!</a></p>"; } else { $size=filesize($_FILES['image']['tmp_name']); if ($size > MAX_SIZE*1024) { $error = "<p>Sorry... You have exceeded the image size limit!</p><p><a href='postRequest.php'>Please try again!</a></p>"; $errors=1; } $image_name=time().'.'.$extension; $newname="images_board/".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { $error = "<p>Sorry... there has been an upload error.</p><p><a href='postRequest.php'>Please try again!</a></p>"; $errors=1; }}}} if(isset($_POST['Submit']) && !$errors) { $error = "<p>Thank You!</p><p>Your request has been submitted</p>"; } //for email... /// $studentName=$_POST['studentName']; $studentId=$_POST['studentId']; $studentEmail=$_POST['studentEmail']; $studentPhone=$_POST['studentPhone']; $postSubject=$_POST['postSubject']; $postDescription=$_POST['postDescription']; $to='email@email.com'; $subject="Bulletin Board Request: $postSubject"; $mainbody.="Student Name: $studentName<br/><br/>"; $mainbody.="Student Id: $studentId<br><br/>"; $mainbody.="Contact email: $studentEmail<br/><br/>"; $mainbody.="Contact Phone: $studentPhone<br/><br/>"; $mainbody.="Subject: $postSubject<br/><br/>"; $mainbody.="Description: $postDescription<br/><br/>"; $mainbody.="Image: $newname<br/>"; $headers="MIME-Version: 1.0\r\n"; $headers.="Content-type:text/html;charset=utf-8\r\n"; $headers.="From: $studentName"; mail($to,$subject,$mainbody,$headers); ?> Quote Link to comment Share on other sites More sharing options...
nafetski Posted March 23, 2010 Share Posted March 23, 2010 Wrap your mail logic in an if statement that checks to see if the $error variable is set? Right now your code checks these things, and adds to the error value...but at the bottom of the page that mail logic isn't wrapped in a conditional. Quote Link to comment Share on other sites More sharing options...
azukah Posted March 23, 2010 Author Share Posted March 23, 2010 i think i solved it .... i submitted a couple of times with big images and i haven't received any emails.. [code=php:0] <?php define ("MAX_SIZE","100"); function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $errors=0; if(isset($_POST['Submit'])) { $image=$_FILES['image']['name']; if ($image) { $filename = stripslashes($_FILES['image']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { $errors=1; // added here $error = "<p>Sorry... only jpg, jpeg, gif and png formats are accepted!</p><p><a href='postRequest.php'>Please try again!</a></p>"; } else { $size=filesize($_FILES['image']['tmp_name']); if ($size > MAX_SIZE*1024) { $errors=1; // moved it here $error = "<p>Sorry... You have exceeded the image size limit!</p><p><a href='postRequest.php'>Please try again!</a></p>"; } $image_name=time().'.'.$extension; $newname="images_board/".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { $errors=1; // moved it here $error = "<p>Sorry... there has been an upload error.</p><p><a href='postRequest.php'>Please try again!</a></p>"; }}}} if(isset($_POST['Submit']) && !$errors) { $error = "<p>Thank You!</p><p>Your request has been submitted</p>"; } //for email... /// $studentName=$_POST['studentName']; $studentId=$_POST['studentId']; $studentEmail=$_POST['studentEmail']; $studentPhone=$_POST['studentPhone']; $postSubject=$_POST['postSubject']; $postDescription=$_POST['postDescription']; $to='email@email.com'; $subject="Bulletin Board Request: $postSubject"; $mainbody.="Student Name: $studentName<br/><br/>"; $mainbody.="Student Id: $studentId<br><br/>"; $mainbody.="Contact email: $studentEmail<br/><br/>"; $mainbody.="Contact Phone: $studentPhone<br/><br/>"; $mainbody.="Subject: $postSubject<br/><br/>"; $mainbody.="Description: $postDescription<br/><br/>"; $mainbody.="Image: $newname<br/>"; $headers="MIME-Version: 1.0\r\n"; $headers.="Content-type:text/html;charset=utf-8\r\n"; $headers.="From: $studentName"; mail($to,$subject,$mainbody,$headers); ?> [/code] Quote Link to comment Share on other sites More sharing options...
JeroenVO Posted March 23, 2010 Share Posted March 23, 2010 You don't need the $errors anymore, this should do it: <?php define ("MAX_SIZE","100"); function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } if(isset($_POST['Submit'])) { $image=$_FILES['image']['name']; if($image) { $filename = stripslashes($_FILES['image']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { $error = "<p>Sorry... only jpg, jpeg, gif and png formats are accepted!</p><p><a href='postRequest.php'>Please try again!</a></p>"; } else { $size=filesize($_FILES['image']['tmp_name']); if ($size > MAX_SIZE*1024) { $error = "<p>Sorry... You have exceeded the image size limit!</p><p><a href='postRequest.php'>Please try again!</a></p>"; } else { $image_name=time().'.'.$extension; $newname="images_board/".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { $error = "<p>Sorry... there has been an upload error.</p><p><a href='postRequest.php'>Please try again!</a></p>"; } else { $error = "<p>Thank You!</p><p>Your request has been submitted</p>"; //for email... /// $studentName=$_POST['studentName']; $studentId=$_POST['studentId']; $studentEmail=$_POST['studentEmail']; $studentPhone=$_POST['studentPhone']; $postSubject=$_POST['postSubject']; $postDescription=$_POST['postDescription']; $to='email@email.com'; $subject="Bulletin Board Request: $postSubject"; $mainbody.="Student Name: $studentName<br/><br/>"; $mainbody.="Student Id: $studentId<br><br/>"; $mainbody.="Contact email: $studentEmail<br/><br/>"; $mainbody.="Contact Phone: $studentPhone<br/><br/>"; $mainbody.="Subject: $postSubject<br/><br/>"; $mainbody.="Description: $postDescription<br/><br/>"; $mainbody.="Image: $newname<br/>"; $headers="MIME-Version: 1.0\r\n"; $headers.="Content-type:text/html;charset=utf-8\r\n"; $headers.="From: $studentName"; mail($to,$subject,$mainbody,$headers); } } } } } ?> Quote Link to comment Share on other sites More sharing options...
azukah Posted March 23, 2010 Author Share Posted March 23, 2010 great! i like your version better .. thanks mate! :D Quote Link to comment 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.