# Form Mail Restrict File Type

**URL:** <https://www.phphelp.com/t/form-mail-restrict-file-type/18894>\
**Category:** General PHP Help\
**Created:** [November 12, 2012, 8:02am UTC](https://www.phphelp.com/t/form-mail-restrict-file-type/18894 "2012-11-12T08:02:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ekleyn](https://www.phphelp.com/user_avatar/www.phphelp.com/ekleyn/32/814_2.png) [@ekleyn](https://www.phphelp.com/u/ekleyn)\
**Post date:** [November 12, 2012, 8:02am UTC](https://www.phphelp.com/t/form-mail-restrict-file-type/18894/1 "2012-11-12T08:02:49Z")

</div>

How would I add a restriction to only allow, word, pdf and notepad document to be sent with this form script.

\<?php if(isset($\_POST['submit'])) { //Deal with the email $to = 'web@protem.co.za'; $message = strip\_tags($\_POST['CV Submitted']); $attachment = chunk\_split(base64\_encode(file\_get\_contents($\_FILES['file']['tmp\_name']))); $filename = $\_FILES['file']['name']; $boundary =md5(date('r', time())); $subject = "CV Upload Form"; $message = "Someone submitted the following information from our cv upload form. Name: ".$\_POST["name"]." Email: ".$\_POST["email"]." Telephone: ".$\_POST["number"]." Sector: ".$\_POST["sector"]." Comments: ".$\_POST["Comment"]." "; $message\_html = " 

Someone submitted the following information from our cv upload form.  
**Name:**".$\_POST["name"]."   
**Email:**".$\_POST["email"]."   
**Telephone:**".$\_POST["number"]."  
**Sector:**".$\_POST["sector"]."   
**Comments:**".$\_POST["comment"]."

"; $headers = "From:admin@protem.co.za\r\nReply-To: admin@protem.co.z"; $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"\_1\_$boundary\""; $message="This is a multi-part message in MIME format. --\_1\_$boundary Content-Type: multipart/alternative; boundary=\"\_2\_$boundary\" --\_2\_$boundary Content-Type: text/plain; charset=\"iso-8859-1\" Content-Transfer-Encoding: 7bit $message --\_2\_$boundary-- --\_1\_$boundary Content-Type: application/octet-stream; name=\"$filename\" Content-Transfer-Encoding: base64 Content-Disposition: attachment $attachment --\_1\_$boundary--"; mail($to, $subject, $message, $headers); header('location:http://www.protem.co.za/cvuploadsuccess.html'); } ?\>

---

<div class="post-metadata">

**Author:** ![zammit1234](https://www.phphelp.com/letter_avatar_proxy/v4/letter/z/f9ae1b/32.png) [@zammit1234](https://www.phphelp.com/u/zammit1234)\
**Post date:** [November 12, 2012, 5:31pm UTC](https://www.phphelp.com/t/form-mail-restrict-file-type/18894/2 "2012-11-12T17:31:56Z")

</div>

Hi you could try somthing like this

[php]

# set the variable name to what ever suits you

$uploadedDoc = $\_FILES[‘file’] [‘name’] ;

// this splits the file name up  
$parts = explode(’.’, $uploadedDoc );// this splits ythe file name up  
// get the file ext  
$fileExt = strtolower(end($parts)); // this gets the extension after the last . and change all the extensions to lower case .

$allowedExt = array(“doc”,“text”,“pdf”);

if(in\_array($fileExt,$allowedExt)) {  
echo "file ext is ok ";  
}else{  
echo “Please only upload .doc,.txt,.pdf”:  
}

[/php]

---

<div class="post-metadata">

**Author:** ![ekleyn](https://www.phphelp.com/user_avatar/www.phphelp.com/ekleyn/32/814_2.png) [@ekleyn](https://www.phphelp.com/u/ekleyn)\
**Post date:** [November 13, 2012, 7:21am UTC](https://www.phphelp.com/t/form-mail-restrict-file-type/18894/3 "2012-11-13T07:21:18Z")

</div>

Thanks that works perfectly 🙂
