# PHP form submission with file upload attachment

**URL:** <https://www.phphelp.com/t/php-form-submission-with-file-upload-attachment/11965>\
**Category:** Code Snippets\
**Created:** [October 12, 2010, 5:06pm UTC](https://www.phphelp.com/t/php-form-submission-with-file-upload-attachment/11965 "2010-10-12T17:06:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kahmai](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/d78d45/32.png) [@kahmai](https://www.phphelp.com/u/kahmai)\
**Post date:** [October 12, 2010, 5:06pm UTC](https://www.phphelp.com/t/php-form-submission-with-file-upload-attachment/11965/1 "2010-10-12T17:06:22Z")

</div>

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 = 'mail@domain.com'; $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; } } ?\>

---

<div class="post-metadata">

**Author:** ![phphelp](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/3ec8ea/32.png) [@phphelp](https://www.phphelp.com/u/phphelp)\
**Post date:** [October 13, 2010, 12:58pm UTC](https://www.phphelp.com/t/php-form-submission-with-file-upload-attachment/11965/2 "2010-10-13T12:58:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kahmai](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/d78d45/32.png) [@kahmai](https://www.phphelp.com/u/kahmai)\
**Post date:** [October 13, 2010, 3:09pm UTC](https://www.phphelp.com/t/php-form-submission-with-file-upload-attachment/11965/3 "2010-10-13T15:09:23Z")

</div>

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?
