Advertisement
michaelyuen

Untitled

May 19th, 2018
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.25 KB | None | 0 0
  1. <form action="#" method="POST" enctype="multipart/form-data" >
  2. <input type="file" name="file[]">
  3. <br>
  4. <input type="file" name="file[]">
  5. <br>
  6. <input type="file" name="file[]">
  7. <br>
  8. <input type="submit" name="upload" value="Upload">
  9. <br>
  10. </form>
  11.  
  12. <?php
  13. function fixFilesArray(&$files) {
  14.     // a mapping of $_FILES indices for validity checking
  15.     $names = array('name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1);
  16.     // iterate over each uploaded file
  17.     foreach ($files as $key => $part) {
  18.     // only deal with valid keys and multiple files
  19.         $key = (string) $key;
  20.         if (isset($names[$key]) && is_array($part)) {
  21.             foreach ($part as $position => $value) {
  22.                 $files[$position][$key] = $value;
  23.             }
  24.             // remove old key reference
  25.             unset($files[$key]);
  26.         }
  27.     }
  28. }
  29.  
  30. if (isset($_POST)) {
  31.  
  32.     fixFilesArray($_FILES['file']);
  33.  
  34.     // email fields: to, from, subject, and so on
  35.     $to = "email@email.com";
  36.     $from = "email@email.com";
  37.     $subject ="My subject";
  38.     $message = "My message";
  39.     $headers = "From: $from";
  40.  
  41.     // boundary
  42.     $semi_rand = md5(time());
  43.     $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  44.  
  45.     // headers for attachment
  46.     $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
  47.  
  48.     // multipart boundary
  49.     $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";
  50.     $message .= "--{$mime_boundary}\n";
  51.  
  52.     foreach ($_FILES['file'] as $file) {
  53.         if (!EMPTY($file['name'])) {
  54.             $fsize = $file['size'];
  55.             $fname = $file['name'];
  56.             $tmp_file = fopen($file['tmp_name'],"rb");
  57.             $data = fread($tmp_file,$fsize);
  58.             fclose($tmp_file);
  59.             $data = chunk_split(base64_encode($data));
  60.             $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$fname\"\n" .
  61.             "Content-Disposition: attachment;\n" . " filename=\"$fname\"\n" .
  62.             "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
  63.             $message .= "--{$mime_boundary}\n";
  64.         }
  65.     }
  66.  
  67.     // send
  68.  
  69.     $ok = @mail($to, $subject, $message, $headers);
  70.     if ($ok) {
  71.     echo "<p>mail sent to $to!</p>";
  72.     } else {
  73.     echo "<p>mail could not be sent!</p>";
  74.     }
  75. }
  76.  
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement