title: Make Attachment File Format only .txt .doc .docx
body: <p>I create a PHP contact form with file attachment with helping a online tutorial. But i want to function it like user only upload .txt .doc .docx file format, no other format will be accepted.</p>
  
  <pre><code>&lt;?php
  $from = "name@example.com";
  $to = $_POST['email'];
  $subject =$_POST['subject'];
  $message = $_POST['body'];
  
  // Temporary paths of selected files
  $file1 = $_FILES['file1']['tmp_name'];
  $file2 = $_FILES['file2']['tmp_name'];
  $file3 = $_FILES['file3']['tmp_name'];
  
  // File names of selected files
  $filename1 = $_FILES['file1']['name'];
  $filename2 = $_FILES['file2']['name'];
  $filename3 = $_FILES['file3']['name'];
  
  // array of filenames to be as attachments
  $files = array($file1, $file2, $file3);
  $filenames = array($filename1, $filename2, $filename3);
  
  // include the from email in the headers
  $headers = "From: $from";
  
  // boundary
  $time = md5(time());
  $boundary = "==Multipart_Boundary_x{$time}x";
  
  // headers used for send attachment with email
  $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$boundary}\"";
  
  // multipart boundary
  $message = "--{$boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
  $message .= "--{$boundary}\n";
  
  // attach the attachments to the message
  for($x=0; $x&lt;count($files); $x++){
      $file = fopen($files[$x],"r");
      $content = fread($file,filesize($files[$x]));
      fclose($file);
      $content = chunk_split(base64_encode($content));
      $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
          "Content-Disposition: attachment;\n" . " filename=\"$filenames[$x]\"\n" .
          "Content-Transfer-Encoding: base64\n\n" . $content . "\n\n";
      $message .= "--{$boundary}\n";
  }
  
  // sending mail
  $sendmail = mail($to, $subject, $message, $headers);
  
  // verify if mail is sent or not
  if ($sendmail) {
      echo "Sent successfully!";
  } else {
      echo "Error occurred. Try again!";
  }
  ?&gt;
  </code></pre>