Hey all!

What I am working on is a contact form with file upload functionality. (e.g. user should be able to upload a screenshot.) Uploading a file should not be mandatory. So the mail which is produced from the entered information in the form should be sent without an attachment in this case. To do this, I have made this code:

if ($_FILES["upload"]["size"] == 0) {
//No file was uploaded. No problem: the mail is being sent without an attachment:
	if ($mail->Send()) {
		echo "Mail sent.<br />";
	} else {
		echo "Error with $mail->Send()";
} elseif ($_FILES["upload"]["size"] > 0) {
	(  ($_FILES["upload"]["type"] == "image/gif")
	|| ($_FILES["upload"]["type"] == "image/jpeg")
	|| ($_FILES["upload"]["type"] == "image/pjpeg")
	|| ($_FILES["upload"]["type"] == "image/png")
	|| ($_FILES["upload"]["type"] == "image/x-png")
	|| ($_FILES["upload"]["type"] == "application/pdf")
	) && ($_FILES["upload"]["size"] < 2097152)
	) {
	// If a file was uploaded and meets the above conditions, do this:
		move_uploaded_file($_FILES['upload']['tmp_name'], $target_path);
		$mail->AddAttachment('uploads/'.basename($_FILES["upload"]["name"]).'', ''.basename( $_FILES["upload"]["name"]).'');
		echo "File successfully uploaded!<br />";

		// Send mail with added attachment:
			if ($mail->Send()) {
				echo "Mail sent with attachment.";
			} else {
				echo "Error sending mail (with attachment)";
	} else {
		echo "Your uploaded file did not meet our conditions. Therefore, the mail was not sent.";

My problem is that this code doesn't quite do what I want:

  • When no file is being uploaded, the mail is sent. So this is OK.
  • When a file is uploaded, which meets the conditions. The mail is sent WITH the attachment. This also is as wanted.
  • When a file is being uploaded which /doesn't/ meet the conditions, the mail is still being sent (what is not what I want!) and without attachment... For some weird reason line 30 is not executed...

Does anybody know what I am doing wrong here? Google didn't give me much love today. :(

5 Years
Discussion Span
Last Post by soul2life

Could you post also the html part? and also whether the file is uploaded to your $targetpath successfully ?

<form enctype="multipart/form-data" action="submitform.php" method="post">
	<table class="contact">
			<td class="contactLabel"><label for="upload"><b>Upload file </b><br />(2MB max.)</label></td>
			<td class="contactUpload"><input type="hidden" name="MAX_FILE_SIZE" value="2097152" />Choose your file: <input name="upload" type="file" /></td>
			<td class="contactLabel"></td>
			<td><input type="submit" value="Send" /></td>

And yes, the file is succesfully uploaded and copied to $targetpath. This piece of code takes care of that:

$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['upload']['name']);

FYI: The file does get sent as attachment when it matches the conditions. My problem is that the email DOES get sent when the file-upload (which should be the email attachment) doesn't meet these conditions. As stated in line 30 of my code in my first post, I don't want the email being sent if the uploaded file doesn't match these conditions.


Whoa, while posting the HTML of the form it came up to me that removing

<input type="hidden" name="MAX_FILE_SIZE" value="2097152" />

could do the trick. It did! :D

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.