title: Upload multiple files with PHPMailer safely
body: <p>I'm building a contact form where users will provide some information and maybe upload multiple images.</p>
  
  <p>I'm using code inspired by <a href="https://github.com/PHPMailer/PHPMailer/blob/master/examples/send_multiple_file_upload.phps" rel="nofollow noreferrer">PHPMailer example to send multiple attachments</a></p>
  
  <p>but unfortunately I see no simple safety checks, (like not trusting MIME, checking max file size, making sure no hidden php script is being injected on my server, etc.)</p>
  
  <p>I have found <a href="http://be2.php.net/manual/en/features.file-upload.php#114004" rel="nofollow noreferrer">here</a> the "correct"/"best" way to make sure I'm safe.. But in my understanding this is just for one file.</p>
  
  <p>So my question is: How to perform this safety checks for an array of $_Files? allowing for 0 or more images, each not exceeding 5MB and a maximum number of images of let's say 4!</p>
  
  <p>For simplicity assume this is my simple <code>html</code></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;head&gt;&lt;/head&gt;
  &lt;body&gt;
      &lt;form method="post" action="action.php" enctype="multipart/form-data"&gt;
          &lt;input type="text" name="name"&gt;
          &lt;input type="email" name="email"&gt;
          &lt;input name="userfile[]" type="file" multiple="multiple"&gt;
          &lt;button type="submit"name="submit" id="submit"&gt;Submit&lt;/button&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>and here is a simple <code>action.php</code> file that is inspired by PHPMailer website but without safety checks!</p>
  
  <pre><code>&lt;?php
  require 'class/PHPMailerAutoload.php';
  
  
  $name = $_POST["name"];
  $email = $_POST["email"];
  $body = "hey";
  
  $mail = new PHPMailer;
  
  $mail-&gt;isSMTP();
  $mail-&gt;Host = 'localhost';
  $mail-&gt;setFrom('mymail@something.com', 'my name');
  $mail-&gt;addAddress('theirmail@something.com', 'their name');
  if (array_key_exists('userfile', $_FILES)) { 
      //Attach multiple files one by one
      for ($ct = 0; $ct &lt; count($_FILES['userfile']['tmp_name']); $ct++) {
          $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name'][$ct]));
          $filename = $_FILES['userfile']['name'][$ct];
          if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
              $mail-&gt;addAttachment($uploadfile, $filename);
          } else {
              $body .= 'Failed to move file to ' . $uploadfile;
          }
      }
  }
  $mail-&gt;Body    = $body;
  $mail-&gt;AltBody = $body;
  
  if(!$mail-&gt;send()) {
      echo ':(!';
      echo 'oops: ' . $mail-&gt;ErrorInfo;
  } else {
      echo: ':)';
  }
  
  ?&gt;
  </code></pre>
  
  <p>How to implement the safety checks for the files?</p>