Form Mail Restrict File Type

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 = '[email protected]'; $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:[email protected]\r\nReply-To: [email protected]"; $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'); } ?>

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]

Thanks that works perfectly :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service