PHP form submission with file upload attachment

I have some code (shown below) that I got from on online tutorial and it works great when the attachment is required, but I cannot figure out how to make this work when the attachment is not required. Any thoughts?

<?php #include_once('/usr/local/lib/php/Mail.php'); include 'Mail.php'; include 'Mail/mime.php'; //Settings $max_allowed_file_size = 1000; // size in KB $allowed_extensions = array("jpg", "jpeg", "gif", "bmp", "png"); $upload_folder = './uploadFolder/'; $your_email = '[email protected]'; $errors =''; if(isset($_POST['Submit'])) { //Get the uploaded file information $name_of_uploaded_file = basename($_FILES['uploaded_file']['name']); //get the file extension of the file $type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1); $size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1024; ///------------Do Validations------------- if($size_of_uploaded_file > $max_allowed_file_size ) { $errors .= "\n The file size is too large!"; } //------ Validate the file extension ----- $allowed_ext = false; for($i=0; $isetTXTBody($text); $message->addAttachment($path_of_uploaded_file); $body = $message->get(); $extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email); $headers = $message->headers($extraheaders); $mail = Mail::factory("mail"); $mail->send($to, $headers, $body); echo ""; } } ///////////////////////////Functions///////////////// // Function to validate against any email injection attempts function IsInjected($str) { $injections = array('(\n+)', '(\r+)', '(\t+)', '(%0A+)', '(%0D+)', '(%08+)', '(%09+)' ); $inject = join('|', $injections); $inject = "/$inject/i"; if(preg_match($inject,$str)) { return true; } else { return false; } } ?>

I believe if you just comment out (or remove) this line in your code, it will work without attachments:
[php]$message->addAttachment($path_of_uploaded_file);[/php]

Are you getting erorr message when trying to submit form without attachment? Probably theire is javascript validation in your html form, that is not included in your post.

Thank you for your reply, but I do still want to be able to include the attachment I just do not want it to be a required field. The error I get when this field is left blank is from the PHP script. This error reads “The uploaded file is not a supported file type!” It seems to me that I need to bypass this step unless there is actually something in the field. Thoughts?

Sponsor our Newsletter | Privacy Policy | Terms of Service