title: Restrict File Type and size in PHP form upload
body: <p>Hi all I have found some really good code over on <a href="http://apptools.com" rel="nofollow">http://apptools.com</a> thats helped me create a form that allows users to enter there details upload a file and it then emails it to me automatically which is great. I just wondered if you knew how I could restrict the file types to doc, pdf, psd, ai, jpg, bmp and maybe a few more in the future and also restrict it to 5mb in size. The code I have is below:</p>
  
  <pre><code>&lt;?php
  if(!isset($_POST['Submit'])){
  echo "error; you need to submit the form!";
  }
  else {
  
  
  $to="my@email.com";
  $subject="Subject header";
  $enquiry = $_POST['message2'];
  $company = $_POST['company2'];
  $tel = $_POST['tel2'];
  $client = $_POST['fromname'];
  $clientemail = $_POST['fromemail'];
  $from = stripslashes($_POST['fromname'])."&lt;".stripslashes($_POST['fromemail'])."&gt;";
  $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
  $headers = "From: $from\r\n" .
  "MIME-Version: 1.0\r\n" .
  "Content-Type: multipart/mixed;\r\n" .
  " boundary=\"{$mime_boundary}\"";
  
  $message="Email from $fromname on  Contact form.\n\n Details are as follows:\n Name:
  $client \n Company: $company \n Email: $clientemail \n Telephone: $tel \n Message: 
  $enquiry \n".
  "\n Please contact them within 48 hours.";
  
  $message = "This is a multi-part message in MIME format.\n\n" .
  "--{$mime_boundary}\n" .
  "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
  "Content-Transfer-Encoding: 7bit\n\n" .
  $message . "\n\n";
  foreach($_FILES as $userfile){
  $tmp_name = $userfile['tmp_name'];
  $type = $userfile['type'];
  $name = $userfile['name'];
  $size = $userfile['size'];
  
  
  if (file_exists($tmp_name)){
  
  
  if(is_uploaded_file($tmp_name)){
  $file = fopen($tmp_name,'rb');
  $data = fread($file,filesize($tmp_name));
  fclose($file);
  $data = chunk_split(base64_encode($data));
  }
  
  $message .= "--{$mime_boundary}\n" .
  "Content-Type: {$type};\n" .
  " name=\"{$name}\"\n" .
  "Content-Disposition: attachment;\n" .
  " filename=\"{$fileatt_name}\"\n" .
  "Content-Transfer-Encoding: base64\n\n" .
  $data . "\n\n";
  }
  }
  $message.="--{$mime_boundary}--\n";
  if (@mail($to, $subject, $message, $headers))
  echo "Message Sent";
  else
  echo "Failed to send";
  }  ?&gt;
  </code></pre>
  
  <p>Any help would be great.</p>